java 死锁代码 Posted on 2020-12-30 | Edited on 2021-02-24 | In java系列 12345678910111213141516171819202122232425262728293031323334353637383940414243package jvm;/** * @author devin * @version 1.0.2 * @date 2021-01-17 14:17 */import java.util.concurrent.TimeUnit;/** * 资源类 */class HoldLockThread implements Runnable{ private String lockA; private String lockB; // 持有自己的锁,还想得到别人的锁 public HoldLockThread(String lockA, String lockB) { this.lockA = lockA; this.lockB = lockB; } @Override public void run() { synchronized (lockA) { System.out.println(Thread.currentThread().getName() + "\t 自己持有" + lockA + "\t 尝试获取:" + lockB); try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lockB) { System.out.println(Thread.currentThread().getName() + "\t 自己持有" + lockB + "\t 尝试获取:" + lockA); } } }} Donate comment here Donate WeChat Pay