案例代码
package first;import java.util.concurrent.TimeUnit;public class DeadLock {private static volatile Object lock = new Object();public static void main(String[] args) {new Thread(() -> {test1();}).start();new Thread(() -> {test2();}).start();}public static void test1() {synchronized (lock) {System.out.printf("test1");try {TimeUnit.DAYS.sleep(1);} catch (InterruptedException e) {throw new RuntimeException(e);}}}public static void test2() {synchronized (lock) {System.out.printf("test2");try {TimeUnit.DAYS.sleep(1);} catch (InterruptedException e) {throw new RuntimeException(e);}}}}
arthas分析
查看阻塞的线程(当前线程持有锁)
thread -b
表示线程12持有的锁,阻塞了其他的线程
查看阻塞的线程(当前线程等待锁)
thread --state BLOCKED