public class Calculator extends Thread {
int total;
public void run(){
synchronized(this){
for(int i=0;i<10;i++)
total +=i;
//notifyAll();
}
}
}
public class Reader extends Thread { /**
* @param args
*/
public static void main(String[] args) {
Calculator c = new Calculator();
new Reader(c).start();
new Reader(c).start();
new Reader(c).start();
c.start(); }
Calculator c;
public Reader(Calculator calc){
c = calc;
}
public void run(){
synchronized(c){
try{
System.out.println("waiting for calculation...");
c.wait();
}catch(InterruptedException e){}
System.out.println("total in reader is: "+c.total);
}
}
}
在运行Reader的时候,程序仍然能输出3个45,可是我已经把Calculator类的notifyAll()给注释掉了,我觉得应该输出不了这个答案了,运行时应该一直在等待才对啊,请各路高手帮忙解释一下,谢谢了!
int total;
public void run(){
synchronized(this){
for(int i=0;i<10;i++)
total +=i;
//notifyAll();
}
}
}
public class Reader extends Thread { /**
* @param args
*/
public static void main(String[] args) {
Calculator c = new Calculator();
new Reader(c).start();
new Reader(c).start();
new Reader(c).start();
c.start(); }
Calculator c;
public Reader(Calculator calc){
c = calc;
}
public void run(){
synchronized(c){
try{
System.out.println("waiting for calculation...");
c.wait();
}catch(InterruptedException e){}
System.out.println("total in reader is: "+c.total);
}
}
}
在运行Reader的时候,程序仍然能输出3个45,可是我已经把Calculator类的notifyAll()给注释掉了,我觉得应该输出不了这个答案了,运行时应该一直在等待才对啊,请各路高手帮忙解释一下,谢谢了!
在它的run运行完后,自然就释放啦锁。
Calculator的run方法执行完是会放弃对c的拥有权,问题是它没有调用notifyAll()的话也就没有启动那3个在等待池中等待的Reader,我就是不明白这3个Reader是怎么又能输出后面的那句打印的
希望再详细讲讲,谢谢!
situations—a thread may wake up even though no code has called notify()
or notifyAll(). (At least, no code you know about has called these methods.
Sometimes the JVM may call notify() for reasons of its own, or code in some other
class calls it for reasons you just don't know.)
有些时候JVM会因为它自己的理由和原因去请求notify(),只是你不知道...
和wait()不同,在对象上调用notify(),并不是立刻放弃对象上的锁并通知等待线程的...而是当我这个代码块执行完才解锁并通知线程...因此,我有理由相信...JVM明白这个对象能解锁了,就表示对该对象的操作已经完毕,即使你没有调用notify(),我也会去通知等待线程...可能是出于JVM的安全机制考虑吧,可能有些时候程序员忘了调了,但明明操作是成功的...导致了异常,代码量巨大的话似乎很难察觉这个问题...当然,这只是我个人观点...毕竟这本书上也没有说明JVM它调用notify()的个人理由是什么...如果你想知道的话,推荐你深入了解下JVM...
最好还是手动的调吧!毕竟自动调是没有保障的,谁也不能够保证...