class Ticket implements Runnable
{
private static int tick = 100;
Object obj = new Object();
public void run()
{
while(true)
{
synchronized(obj)//上锁。
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"---sale : "+tick--);
}
}
}
}
}
class TicketDemo1
{
public static void 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();
求大神
|
|