public class TestTicket_1
{
public static void main(String[] args)
{
Ticket a = new Ticket();
Thread T1 = new Thread(a);
Thread T2 = new Thread(a);
Thread T3 = new Thread(a);
Thread T4 = new Thread(a);
T1.start();
T2.start();
T3.start();
T4.start();
}
}
class Ticket implements Runnable
{
Object obj = new Object();
private int tickets = 100;
public void run()
{
while(true)
{
synchronized(obj)
{
if(tickets > 0)
{
try{Thread.sleep(100);}catch(Exception e){}
System.out.println(Thread.currentThread().getName() + "卖出了" + tickets--);
}
}
}
}
}