67,513
社区成员
发帖
与我相关
我的任务
分享
public void run() {
synchronized (o) {
if (0 == i) {
System.out.println("出来吧~~" + Thread.currentThread().getName() + " " + i);
}
while (i > 0) {
try {
Thread.sleep(10);
System.out.println("出来吧~~" + Thread.currentThread().getName() + " " + i);
i--;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
while (i > 0) {// <<<-----这里while(i>0)不在同步区块,因此在底下同步区块阴塞时,代码还是能进入这个循环,把 synchronized (o)提到while前就可以了
synchronized (o) {
try {
Thread.sleep(10);
System.out.println("出来吧~~" + Thread.currentThread().getName() + " " + i);
i--;
} catch (Exception e) {
e.printStackTrace();
}
}
}