[Java] 纯文本查看 复制代码
class Window extends Thread{
static int num = 1;
public Window(String name){
super(name);
}
@Override
public void run() {
for(;num <= 50; num++){
try{
synchronized ("锁"){
this.sleep(100);
System.out.println(Thread.currentThread().getName() + "售出" + num + "号");
}
}catch(InterruptedException e){};
}
}
}
public class ticket {
public static void main(String[] args) {
Window w1 = new Window("窗口1");
Window w2 = new Window("窗口2");
Window w3 = new Window("窗口3");
w1.start();
w2.start();
w3.start();
}
}
为什么会出现51,52张票,是因为for的原因吗?while就没有