public class ThreadDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
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();
}
}
class Ticket implements Runnable {
private int ticket = 100;
Ticket(){ }
public void run(){
while(true){
Object obj = new Object();
synchronized (obj)
{
if(ticket>0)
{
try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();} // TODO Auto-generated catch block
System.out.println(Thread.currentThread().getName()+"..."+ticket--);
}
}
}
}
}
|
|