class Ticket implements Runnable
{
private int tick=1000;
Object obj=new Object();
public void run()
{
while(true)
{
synchronized(obj)
{
if(tick>0)
{
try
{
Thread.sleep(10);
}
catch (Exception e)
{
System.out.println("执行");
}
System.out.println(Thread.currentThread().getName()+tick--);
}
}
}
}
}
class TicketDemo2
{
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();
}
}
下面是打印的一段
Thread-0385
Thread-0384
Thread-0383
Thread-0382
Thread-0381
Thread-0380
Thread-0379
Thread-0378
Thread-0377
Thread-3376
Thread-3375
Thread-3374
Thread-3373
Thread-3372
Thread-3371
Thread-3370
Thread-3369
1.为什么打印会从300多到3000多?
2.run方法下面的while(true)是什么意思?
3.我这个程序定义了三个Thread,为什么打印没有显示啊?
求解
|