class Ticket implements Runnable
{
private int tick=100;
Object obj=new Object();
int count=0;
public void run()
{
while(true)
{
synchronized(obj)
{
if(tick>0)
{
try
{
Thread.sleep(20);
}
catch (Exception e)
{
}
System.out.println(Thread.currentThread().getName()+"售出第"+tick--+"张票");
count++;
}else
return;
}
}
System.out.println(count);
}
}
class Demo
{
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);
t2.start();
t3.start();
t4.start();
t1.start();
}
}
我的本意是想测试一下,打印的票数是否是100张,所以用了计数,编译错误,求教原因。
|