本帖最后由 .____盒子 于 2014-7-7 16:17 编辑
请教多线程问题 , 该程序为何不能正常打印出三个窗口正确的线程 ? ? ?
public class TicketDemo {
public static void main(String[] args) {
// 创建自定义线程
ticket m = new ticket();
//
Thread th1 = new Thread(m, "窗口1");
Thread th2 = new Thread(m, "窗口2");
Thread th3 = new Thread(m, "窗口3");
// 调用start()方法
th1.start();
th2.start();
th3.start();
}
}
public class ticket implements Runnable {
private int ticket =100;
@Override
public void run() {
// 定义票数
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(ticket>0){
System.out.println(Thread.currentThread().getName()+"- - "+ticket+"余票--");
}
}
}
}
|