有一个买票的小程序;
为什么用这个Runnable 接口子类对象建立的多线程,怎么只有一个线程从头买到尾呢?
class Tictke entends Runnable{
prinvate int tickNum=100; //有100张票;
public synchronized void run(){ //同步函数中的买票语句;
while(true){
if(tickNum>0){
System.out.println(Thread.currentThread().getName()+"tick::"+tickNum--);
}
}
}
}
--------------------------------
main(String[] args){
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
|
|