public class ManyDemo implements Runnable{
private int ticket=1000;
public static void main(String[] args)throws Exception {
ManyDemo md=new ManyDemo();
Thread t1=new Thread(md);
Thread t3=new Thread(md);
Thread t4=new Thread(md);
Thread t2=new Thread(md);
t1.start();
t2.start();
t3.start();
t4.start();
}
public void run() {
while(true){
show();
}
}
public synchronized void show(){
if(ticket>0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"正在卖["+(ticket--)+"]号票");
}
}
}
|