返回信息流public class DeadLock extends java.lang.Thread
{
private byte[] o1=new byte[0];
private byte[] o2=new byte[0];
public void lock1() throws Exception
{
synchronized(o1)
{
Thread.sleep(1000);
System.out.println("lock1 get the lock of o1");
synchronized(o2)
{
Thread.sleep(1000);
System.out.println("lock1 get the lock of o2");
}
System.out.println("lock1 leave the lock of o2");
}
System.out.println("lock1 leave the lock of o1");
}
public void lock2() throws Exception
{
synchronized(o2)
{
Thread.sleep(1000);
System.out.println("lock2 get the lock of o2");
synchronized(o1)
{
Thread.sleep(1000);
System.out.println("lock2 get the lock of o1");
}
System.out.println("lock2 leave the lock of o1");
}
System.out.println("lock2 leave the lock of o2");
}
public void run()
{
try{
synchronized(DeadLock.class){
sleep(50);
InnerClass.addI();
}
if(InnerClass.getI()%2==0)
{
System.out.println("i"+InnerClass.getI());
lock1();
}
else
{
System.out.println("i"+InnerClass.getI());
lock2();
}
InnerClass.addI();
}catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
(new DeadLock()).start();
Thread.sleep(500);
(new DeadLock()).start();
}
}
执行结果是
i1
i2
lock2 get the lock of o2
lock1 get the lock of o1
lock2 get the lock of o1
lock2 leave the lock of o1
lock2 leave the lock of o2
lock1 get the lock of o2
lock1 leave the lock of o2
lock1 leave the lock of o1
为什么不死呢?[ema0]
这是一条镜像帖。来源:北邮人论坛 / java / #20020同步于 2011/9/9
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
我写了个死锁,怎么不死呢--求大牛
mxlwd168
2011/9/9镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
改成lock对象的也是不死 code:
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class DeadLock2 extends java.lang.Thread
{
Lock lockone = new ReentrantLock();
Lock locktwo = new ReentrantLock();
public void lock1() throws Exception
{
lockone.lock();
Thread.sleep(1000);
System.out.println("lock1 get the lock of lockone");
locktwo.lock();
Thread.sleep(1000);
System.out.println("lock1 get the lock of locktwo");
locktwo.unlock();
System.out.println("lock1 leave the lock of locktwo");
lockone.unlock();
System.out.println("lock1 leave the lock of lockone");
}
public void lock2() throws Exception
{
locktwo.lock();
Thread.sleep(1000);
System.out.println("lock2 get the lock of locktwo");
lockone.lock();
Thread.sleep(1000);
System.out.println("lock2 get the lock of lockone");
lockone.unlock();
System.out.println("lock2 leave the lock of lockone");
locktwo.unlock();
System.out.println("lock2 leave the lock of locktwo");
}
public void run()
{
try{
synchronized(DeadLock.class){
sleep(50);
InnerClass.addI();
}
if(InnerClass.getI()%2==0)
{
System.out.println("i"+InnerClass.getI());
lock1();
}
else
{
System.out.println("i"+InnerClass.getI());
lock2();
}
InnerClass.addI();
}catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
(new DeadLock()).start();
Thread.sleep(500);
(new DeadLock()).start();
}
}
懒得看逻辑了
main函数
DeadLock dl = new DeadLock();
new Thread(dl).start;
Thread.sleep(500);
new Thread(dl).start;
当然死不了。
lz创建的两个线程,各自创造了两个锁对象。也就是说,每人都有两个自己的锁,两人加起来是4把锁。这样肯定不会死的。
看看我这个程序:这个一般情况下都能死锁。
package deadlocktest;
class Locker extends Thread {
private Object firstToLock;
private Object secondToLock;
public Object getFirstToLock() {
return firstToLock;
}
public void setFirstToLock(Object firstToLock) {
this.firstToLock = firstToLock;
}
public Object getSecondToLock() {
return secondToLock;
}
public void setSecondToLock(Object secondToLock) {
this.secondToLock = secondToLock;
}
@Override
public void run() {
try {
System.out.format("Thread %s getting lock %s...\n", getName(),
firstToLock);
synchronized (firstToLock) {
System.out.format("Thread %s got lock %s!\n", getName(),
firstToLock);
Thread.sleep(1000);
System.out.format("Thread %s getting lock %s!\n", getName(),
secondToLock);
synchronized (secondToLock) {
System.out.format("Thread %s got lock %s!\n", getName(),
secondToLock);
Thread.sleep(1000);
System.out.format("Thread %s releasing lock %s!\n",
getName(), secondToLock);
}
System.out.format("Thread %s releasing lock %s!\n", getName(),
firstToLock);
}
} catch (InterruptedException e) {
System.out.println("Someone says we should stop.");
}
}
}
public class DeadLockTest {
public static void main(String[] args) throws InterruptedException {
String lock1 = "foo";
String lock2 = "bar";
Locker locker1 = new Locker();
Locker locker2 = new Locker();
locker1.setName("locker1");
locker1.setFirstToLock(lock1);
locker1.setSecondToLock(lock2);
locker2.setName("locker2");
locker2.setFirstToLock(lock2);
locker2.setSecondToLock(lock1);
locker1.start();
Thread.sleep(500);
locker2.start();
}
}