书上同步例子没有出现他说的错误
第一次发帖不会弄书上说延迟会出现的情况,我运行了没出现啊
·······
ticket=1
ticket=0
ticket=-1
代码:
class MyThread implements Runnable {
private int ticket = 5;
public void run() {
for (int i = 0; i < 100; i++) {
if (ticket > 0) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "ticket="
+ ticket--);
}
}
}
}
public class SyncDemo01 {
public static void main(String[] agrs) {
Thread m1 = new Thread(new MyThread(), "一号窗");
Thread m2 = new Thread(new MyThread(), "二号窗");
Thread m3 = new Thread(new MyThread(), "三号窗");
m1.start();
m2.start();
m3.start();
}
}
谢谢各位啊,苦逼大学生自学java中。。。。。。。。