//死锁
class Ticket implements Runnable
{
int ticket=1000;
boolean flag=true;
Object obj=new Object();
public void run()
{
if(flag)
{
while(true)
{
synchronized(obj)
{
show();
}
}
}else{
while(true)
{
show();
}
}
}
synchronized void show()
{
synchronized(obj)
{
if(ticket>0)
System.out.println(Thread.currentThread().getName()+"---"+ticket--);
}
}
}
public class SiSuo
{
public static void main(String[] args)
{
Ticket t=new Ticket();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.start();
try{Thread.sleep(10);}catch(Exception e){}
t.flag=false;
t2.start();
}
} |
|