本帖最后由 王红霞 于 2012-7-18 12:20 编辑
class Ticket implements Ruunable
{
private int tick=100;
Object obj=new Object();
public void run()
{
while(ture)
{
synchronized(obj)
{
if(tick>0)
{
try(Thread.sleep(2);)catch(Exception e){}//既然线程是同步 为什么还要sleep?我觉得就算是有毕老师讲的例子的那种情况可是完全没必要加sleep啊?不用sleep不行吗?
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);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
|