/*** 死锁demo*/
public class DeadlockDemo {public static void main(String[] args) {// 创建两个对象final Object resource1 = "resource1";final Object resource2 = "resource2";// 创建第一个线程Thread t1 = new Thread(() -> {// 尝试锁定resource1synchronized (resource1) {System.out.println("Thread 1: locked resource 1");try {// 使线程睡眠一段时间Thread.sleep(100);} catch (Exception e) {e.printStackTrace();}// 尝试锁定resource2synchronized (resource2) {System.out.println("Thread 1: locked resource 2");}}});// 创建第二个线程Thread t2 = new Thread(() -> {// 尝试锁定resource2synchronized (resource2) {System.out.println("Thread 2: locked resource 2");try {// 使线程睡眠一段时间Thread.sleep(100);} catch (Exception e) {e.printStackTrace();}// 尝试锁定resource1synchronized (resource1) {System.out.println("Thread 2: locked resource 1");}}});// 启动两个线程t1.start();t2.start();}
}
查看
方式1:
1) 命令 gps 查看pid
2)记下pid 使用 jstack 命令查看
方式2:
使用jconsole命令打开jdk的监控控制台查看