黑马程序员技术交流社区
标题:
java3个窗口售票
[打印本页]
作者:
yihaiyang
时间:
2016-8-13 17:50
标题:
java3个窗口售票
[mw_shl_code=java,true]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();
}
}
[/mw_shl_code]为什么会出现51,52张票,是因为for的原因吗?while就没有
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2