黑马程序员技术交流社区
标题:
求教,刚看到线程,关于死锁的运行过程。
[打印本页]
作者:
何建明
时间:
2014-3-17 14:20
标题:
求教,刚看到线程,关于死锁的运行过程。
class Ticket implements Runnable
{
private int num = 100;
Object obj = new Object();
boolean flag = true;
public void run()
{
if(flag)
while(true)
{
synchronized(obj)
{
show();
}
}
else
while(true)
this.show();
}
public synchronized void show()
{
synchronized(obj)
{
if(num>0)
{
try{Thread.sleep(10);}catch (InterruptedException e){}
System.out.println(Thread.currentThread().getName()+".....sale...."+num--);
}
}
}
}
class Demo71
{
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(InterruptedException e){}
t.flag = false;
t2.start();
}
}
复制代码
上面代码,的运行产生死锁的运行过程是怎么样的?能否比较详细的罗列一个运行步骤,1 2 3 这样的步骤。
先谢过。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2