点击领取优惠~
523 字
3 分钟
线程死锁条件
线程死锁条件
死锁是如何发生的,如何避免死锁?
当线程A持有独占锁a,并尝试去获取独占锁b的同时,线程B持有独占锁b,并尝试获取独占锁a的情况下,就会发生AB两个线程由于互相持有对方需要的锁,而发生的阻塞现象,我们称为死锁。
public class DeadLockDemo {
public static void main(String[] args) { // 线程a Thread td1 = new Thread(new Runnable() { public void run() { DeadLockDemo.method1(); } }); // 线程b Thread td2 = new Thread(new Runnable() { public void run() { DeadLockDemo.method2(); } });
td1.start(); td2.start(); }
public static void method1() { synchronized (String.class) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程a尝试获取integer.class"); synchronized (Integer.class) {
} } }
public static void method2() { synchronized (Integer.class) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程b尝试获取String.class"); synchronized (String.class) {
} } }}造成死锁的四个条件:
- 互斥条件:一个资源每次只能被一个线程使用。
- 请求与保持条件:一个线程因请求资源而阻塞时,对已获得的资源保持不放。
- 不剥夺条件:线程已获得的资源,在未使用完之前,不能强行剥夺。
- 循环等待条件:若干线程之间形成一种头尾相接的循环等待资源关系。
在并发程序中,避免了逻辑中出现数个线程互相持有对方线程所需要的独占锁的的情况,就可以避免死锁,如下所示:
public class BreakDeadLockDemo {
public static void main(String[] args) { // 线程a Thread td1 = new Thread(new Runnable() { public void run() { DeadLockDemo2.method1(); } }); // 线程b Thread td2 = new Thread(new Runnable() { public void run() { DeadLockDemo2.method2(); } });
td1.start(); td2.start(); }
public static void method1() { synchronized (String.class) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程a尝试获取integer.class"); synchronized (Integer.class) { System.out.println("线程a获取到integer.class"); }
} }
public static void method2() { // 不再获取线程a需要的Integer.class锁。 synchronized (String.class) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程b尝试获取Integer.class"); synchronized (Integer.class) { System.out.println("线程b获取到Integer.class"); } } }} 线程死锁条件