本帖最后由 吹血弄花 于 2014-4-12 20:49 编辑
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 TicketDemo
{
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);
/*
Ticket t1=new Ticket();
Ticket t2=new Ticket();
Ticket t3=new Ticket();
Ticket t4=new Ticket();*/
t1.start();
t2.start();
t3.start();
t4.start();
}
}
照着教程一步步敲出来的,为什么我的只有0线程打印啊?教程中是4个线程都打印的。 |