【发布时间】:2017-11-04 03:02:38
【问题描述】:
有两个线程。一个是操纵 x,另一个是显示 x。如何使用同步、中断、等待和通知来实现互斥。图片显示了这一点。
我想出了这个解决方案,但我不确定它是否正确。
写
synchronized(x){
x = x + 1;
notify();
try{
wait();
}
catch(InterruptedException e){
}
}
阅读
synchronized(x){
try{
wait();
}
catch(InterruptedException e){
}
System.out.print(x);
}
【问题讨论】:
标签: multithreading synchronization wait notify mutual-exclusion