【发布时间】:2014-07-17 03:18:06
【问题描述】:
我有一个简单的程序,我发现它非常混乱。代码sn-p如下:
class Processor{
public void produce() Throws InterruptedException{
synchronized(this){
System.out.println("Producer Running...");
wait();
System.out.println("Resumed");
}
}
public void consume() Throws InterruptedException{
synchronized(this){
Thread.Sleep(2000);
System.out.println("Consumer Running... Press return key to return");
scan.nextLine();
notify();
Thread.sleep(5000);
}
}
现在我的问题是,当我们在“produce”方法中调用 wait() 时,执行会立即转移到“consume”方法。 (生产和消费在不同的线程中执行)。但是当 notify();在“消费”方法中被调用,执行不会立即转移。它等待 Thread.sleep(5000) 完成。为什么会这样?
【问题讨论】:
标签: java multithreading sleep wait notify