0
有两个线程。一个是操纵x,另一个是显示x。如何使用同步,中断,等待和通知来互相排斥。图像显示了这一点。互斥使用等待,同步,中断和通知
我已经想出了这个解决方案,但如果它的正确IAM不能肯定。
写
synchronized(x){
x = x + 1;
notify();
try{
wait();
}
catch(InterruptedException e){
}
}
读
synchronized(x){
try{
wait();
}
catch(InterruptedException e){
}
System.out.print(x);
}