引言
本文简单介绍Jconsole,死锁用例测试分析线程
正文
测试用例
DeadLockDemo :
public class DeadLockDemo {private static Object resource1 = new Object();//资源 1private static Object resource2 = new Object();//资源 2public static void main(String[] args) {new Thread(() -> {synchronized (resource1) {System.out.println(Thread.currentThread() + "get resource1");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread() + "waiting get resource2");synchronized (resource2) {System.out.println(Thread.currentThread() + "get resource2");}}}, "线程 1").start();new Thread(() -> {synchronized (resource2) {System.out.println(Thread.currentThread() + "get resource2");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread() + "waiting get resource1");synchronized (resource1) {System.out.println(Thread.currentThread() + "get resource1");}}}, "线程 2").start();}
}
Jconsole
Jconsole是Java 监视与管理控制台,是JDK自带的软件
开始测试
下面是执行测试用例的结果
打开Jconsole后默认是本地分析,选择要分析的进程,直接链接就行,下面的是远程连接
点击线程,点击检测死锁
结果
会把死锁代码贴出来
结语
更详细的使用操作可以自行百度