风也温柔

计算机科学知识库

java线程死锁例子 有遇到java死锁情况的小伙伴吗

  1. 产生条件

  一般来说,要出现死锁问题需要满足以下条件:

  互斥条件:一个资源每次只能被一个线程使用。请求与保持条件:一个线程因请求资源而阻塞时,对已获得的资源保持不放。不剥夺条件:线程已获得的资源,在未使用完之前,不能强行剥夺。循环等待条件:若干线程之间形成一种头尾相接的循环等待资源关系。

  在JAVA编程中java线程死锁例子,有3种典型的死锁类型:

  下面来一一介绍这三种死锁:

  2. 静态的锁顺序死锁

  举个例子:

  <pre class="prettyprint">
`//可能发生静态锁顺序死锁的代码
class StaticLockOrderDeadLock {

private final Object lockA = new Object();
private final Object lockB = new Object();
public void a() {
    synchronized (lockA) {
        synchronized (lockB) {
            System.out.println("function a");
        }
    }
}

public void b() {
    synchronized (lockB) {
        synchronized (lockA) {
            System.out.println("function b");
        }
    }
}

}
`</pre>

  解决办法: 所有需要多个锁的线程java线程死锁例子,都要以相同的顺序来获得锁。

  <pre class="prettyprint">
`//正确的代码
class StaticLockOrderDeadLock {

private final Object lockA = new Object();
private final Object lockB = new Object();
public void a() {
    synchronized (lockA) {
        synchronized (lockB) {
            System.out.println("function a");
        }
    }
}

public void b() {
    synchronized (lockA) {
        synchronized (lockB) {
            System.out.println("function b");
        }
    }
}

}
`</pre>

  3. 动态的锁顺序死锁

  动态的锁顺序死锁是指两个线程调用同一个方法时,传入的参数颠倒造成的死锁。

  举个例子:

  <pre class="prettyprint">
`//可能发生动态锁顺序死锁的代码
class DynamicLockOrderDeadLock {

public void transefMoney(Account fromAccount, Account toAccount, Double amount) {
    synchronized (fromAccount) {
        synchronized (toAccount) {
            //...
            fromAccount.minus(amount);
            toAccount.add(amount);
            //...
        }
    }
}

}
`</pre>

  解决方案:使用.来定义锁的顺序。确保所有的线程都以相同的顺序获得锁。

  <pre class="prettyprint">
`class DynamicLockOrderDeadLock {

private final Object myLock = new Object();
public void transefMoney(final Account fromAccount, final Account toAccount, final Double amount) {
    class Helper {
        public void transfer() {
        //...
        fromAccount.minus(amount);
        toAccount.add(amount);
        //...
        }
    }
    int fromHash = System.identityHashCode(fromAccount);
    int toHash = System.identityHashCode(toAccount);
    if (fromHash  toHash) {
        synchronized (toAccount) {
            synchronized (fromAccount) {
                new Helper().transfer();
            }
        }
    } else {
        synchronized (myLock) {
            synchronized (fromAccount) {
                synchronized (toAccount) {
                    new Helper().transfer();
                }
            }
        }
    }
    
}

}
`</pre>

  4. 协作对象之间发生的死锁

  有时,死锁并不会那么明显,比如两个相互协作的类之间的死锁

  举个例子:

  <pre class="prettyprint">
`//可能发生死锁
class Taxi {

private Point location, destination;
private final Dispatcher dispatcher;
public Taxi(Dispatcher dispatcher) {
    this.dispatcher = dispatcher;
}

public synchronized Point getLocation() {
    return location;
}
public synchronized void setLocation(Point location) {
    this.location = location;
    if (location.equals(destination))
        dispatcher.notifyAvailable(this);//外部调用方法,可能等待Dispatcher对象锁
}

}
class Dispatcher {

private final Set taxis;
private final Set availableTaxis;
public Dispatcher() {
    taxis = new HashSet();
    availableTaxis = new HashSet();
}

public synchronized void notifyAvailable(Taxi taxi) {
    availableTaxis.add(taxi);
}
public synchronized Image getImage() {
    Image image = new Image();
    for (Taxi t : taxis)
        image.drawMarker(t.getLocation());//外部调用方法,可能等待Taxi对象锁
    return image;
}

}
`</pre>

  我们在持有锁的情况下调用了外部的方法,这是非常危险的(可能发生死锁)。为了避免这种危险的情况发生java线程死锁例子 有遇到java死锁情况的小伙伴吗, 我们使用开放调用。如果调用某个外部方法时不需要持有锁,我们称之为开放调用。

  解决方案:需要使用开放调用,即避免在持有锁的情况下调用外部的方法。

  <pre class="prettyprint">
`//正确的代码
class Taxi {

private Point location, destination;
private final Dispatcher dispatcher;
public Taxi(Dispatcher dispatcher) {
    this.dispatcher = dispatcher;
}
public synchronized Point getLocation() {
    return location;
}
public void setLocation(Point location) {
    boolean flag = false;
    synchronized (this) {
        this.location = location;
        flag = location.equals(destination);
    }
    if (flag)
        dispatcher.notifyAvailable(this);//使用开放调用
}

}
class Dispatcher {

private final Set taxis;
private final Set availableTaxis;
public Dispatcher() {
    taxis = new HashSet();
    availableTaxis = new HashSet();
}
public synchronized void notifyAvailable(Taxi taxi) {
    availableTaxis.add(taxi);
}
public Image getImage() {
    Set copy;
    synchronized (this) {
        copy = new HashSet(taxis);
    }
    Image image = new Image();
    for (Taxi t : copy)
        image.drawMarker(t.getLocation());//使用开放调用
    return image;
}
pre>

  文章来源:http://www.codeforest.cn/article/2252