本帖最后由 黑龙江—刘鹏 于 2012-8-28 20:12 编辑
- <DIV class=blockcode>
- <BLOCKQUOTE>class DeadLock implements Runnable
- { Object d =new Object();
- Object d1 =new Object();
- private boolean panduan;
- DeadLock(boolean panduan)
- {
- this.panduan=panduan;
- }
- public void run()
- {
- while(true)
- {
- if(panduan)
- {
- synchronized(d)
- {
- System.out.println("liupeng __d");
- synchronized(d1)
- {
- System.out.println("liupeng___d1");
- }
- panduan = false;
- }
- }
- else
- {
- while(true)
- {
- synchronized(d1)
- {
- System.out.println("liupeng ___d__1");
- synchronized(d)
- {
- System.out.println("liupeng___d1___1");
- }
- panduan = true;
- }
- }
- }
- }
- }
- }
- class DeadLockDemo
- {
- public static void main(String[] args)
- {
- DeadLock d = new DeadLock(true);
- Thread t = new Thread(d);
- Thread t1= new Thread(d);
- t.start();
- t1.start();
- }
- }
复制代码 <P>class DeadLock implements Runnable
{ Object d =new Object();
Object d1 =new Object();
private boolean panduan;
DeadLock(boolean panduan)
{
this.panduan=panduan;
}</P>
<P> public void run()
{
if(panduan)
{
while(true)
{
synchronized(d)
{
System.out.println("liupeng __d");
synchronized(d1)
{
System.out.println("liupeng___d1");
}
}
}
}
else
{
while(true)
{
synchronized(d1)
{
System.out.println("liupeng ___d__1");
synchronized(d)
{
System.out.println("liupeng___d1___1");
}
}
}
}
}
}</P>
<P>class DeadLockDemo
{
public static void main(String[] args)
{
Thread t = new Thread(new DeadLock(true));
Thread t1= new Thread(new DeadLock(false));
Thread t2= new Thread(new DeadLock(false));
t.start();
t1.start();
}
}</P>这个 为什么运行没有出现死锁效果呢?
更改后的程序如上 可以死锁 谢谢啦
不知道为什么缩进完全不见了
sorry 太吗不好读了 |
|