class Ticket implements Runnable
{
private static int tick = 100;
Object o = new Object();
public void run()
{
while(true)
{
synchronized(o)
{
if(tick>0)
{
try{Thread.sleep(15);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"...tick..."+(tick--));
}
}
}
}
}
public class TicketSell2
{
public static void main(String[] args)
{
Ticket t1 = new Ticket();
Ticket t2 = new Ticket();
Ticket t3 = new Ticket();
Ticket t4 = new Ticket();
Thread tt1 = new Thread(t1);
Thread tt2 = new Thread(t2);
Thread tt3 = new Thread(t3);
Thread tt4 = new Thread(t4);
tt1.start();
tt2.start();
tt3.start();
tt4.start();
}
}
*********************************************************************************
class Ticket implements Runnable
{
private static int tick = 100;
Object o = new Object();
public void run()
{
while(true)
{
synchronized(o)
{
if(tick>0)
{
try{Thread.sleep(15);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"...tick..."+(tick--));
}
}
}
}
}
public class TicketSell2
{
public static void main(String[] args)
{
Ticket t1 = new Ticket();
Thread tt1 = new Thread(t1);
Thread tt2 = new Thread(t1);
Thread tt3 = new Thread(t1);
Thread tt4 = new Thread(t1);
tt1.start();
tt2.start();
tt3.start();
tt4.start();
}
}
新建一个Thread使用同一个Runnable子类对象 和使用不同的Runable子类对象有什么区别呢 ??
|