以下用一个实例来构造这样的情况:
package basic.e_deadlock; import org.apache.log4j.Logger; public class TestDeadLock { public static void main(String[] args) { DeadlockRisk dead = new DeadlockRisk(); MyThread t1 = new MyThread(dead, 1, 2, "线程1"); MyThread t2 = new MyThread(dead, 3, 4, "线程2"); MyThread t3 = new MyThread(dead, 5, 6, "线程3"); MyThread t4 = new MyThread(dead, 7, 8, "线程4"); t1.start(); t2.start(); t3.start(); t4.start(); } } class MyThread extends Thread { private DeadlockRisk dead; private int a, b; MyThread(DeadlockRisk dead, int a, int b, String threadName) { this.dead = dead; this.a = a; this.b = b; this.setName(threadName); } @Override public void run() { dead.read(); dead.write(a, b); } } class DeadlockRisk { private static Logger logger = Logger.getLogger(DeadlockRisk.class); private static class Resource { public int value; } private Resource resourceA = new Resource(); private Resource resourceB = new Resource(); public void read() { logger.debug("===========read begin==========="); synchronized (resourceA) { logger.debug("read():" + Thread.currentThread().getName() + "获取了resourceA 的锁!"); synchronized (resourceB) { logger.debug("read():" + Thread.currentThread().getName() + "获取了resourceB 的锁!"); } } logger.debug("===========read end============="); } public void write(int a, int b) { logger.debug("===========write begin==========="); synchronized (resourceB) { logger.debug("write():" + Thread.currentThread().getName() + "获取了resourceB 的锁!"); synchronized (resourceA) { logger.debug("write():" + Thread.currentThread().getName() + "获取了resourceA 的锁!"); resourceA.value = a; resourceB.value = b; } } logger.debug("===========write end============="); } }
运行结果:
0 [线程1] DEBUG basic.e_deadlock.DeadlockRisk - ===========read begin=========== 0 [线程2] DEBUG basic.e_deadlock.DeadlockRisk - ===========read begin=========== 0 [线程4] DEBUG basic.e_deadlock.DeadlockRisk - ===========read begin=========== 0 [线程3] DEBUG basic.e_deadlock.DeadlockRisk - ===========read begin=========== 0 [线程1] DEBUG basic.e_deadlock.DeadlockRisk - read():线程1获取了resourceA 的锁! 0 [线程1] DEBUG basic.e_deadlock.DeadlockRisk - read():线程1获取了resourceB 的锁! 0 [线程1] DEBUG basic.e_deadlock.DeadlockRisk - ===========read end============= 0 [线程2] DEBUG basic.e_deadlock.DeadlockRisk - read():线程2获取了resourceA 的锁! 0 [线程1] DEBUG basic.e_deadlock.DeadlockRisk - ===========write being=========== 1 [线程2] DEBUG basic.e_deadlock.DeadlockRisk - read():线程2获取了resourceB 的锁! 1 [线程2] DEBUG basic.e_deadlock.DeadlockRisk - ===========read end============= 1 [线程2] DEBUG basic.e_deadlock.DeadlockRisk - ===========write being=========== 1 [线程1] DEBUG basic.e_deadlock.DeadlockRisk - write():线程1获取了resourceB 的锁! 1 [线程4] DEBUG basic.e_deadlock.DeadlockRisk - read():线程4获取了resourceA 的锁!
注意:此时线程1在等待resourceB的资源,线程2在等待resourceA的资源。两个线程在相互等待,出现死锁。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/119121.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...