多线程练习(为什么运行后只有线程1一直在发书)

 

package Five;

public class Multithreaded implements Runnable{
    private int book = 10;
    Object lock = new Object();
    public void run() {
        while(true) {
            synchronized(lock) {
                if(book>0) {
                    try {
                        Thread.sleep(1000);
                    }catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"正在发第"+book--+"本书");
                }
            }
        }
    }
    public static void main(String[] args) {
        Multithreaded mu = new     Multithreaded();
        new Thread(mu,"老师 1").start();
        new Thread(mu,"老师 2").start();
        new Thread(mu,"老师 3").start();
        new Thread(mu,"老师 4").start();
        
    }

}
 

你那是死循环

应为使用了同步锁。