问题描述:
窗口1 和窗口3 问什么不显示?
为什么和:
t2.start();
t3.start();
t1.start();
的摆放先后顺序有关呢?
不应该是 三个线程都执行 然后随机出不一样的窗口吗?
以下是相关代码
class window1 implements Runnable {
private int ticke = 100;
Object obj=new Object();
@Override
public void run() {
while (true) {
synchronized (obj) {
if (ticke > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "卖票: 票号为:" + ticke);
ticke--;
} else {
break;
}
}
}
}
}
public class WindowTest1 {
public static void main(String[] args) {
window1 w = new window1();
Thread t1 = new Thread(w);
Thread t2 = new Thread(w);
Thread t3 = new Thread(w);
t2.setName("窗口2] ");
t1.setName("窗口1] ");
t3.setName("窗口3] ");
t2.start();
t3.start();
t1.start();
}
}