死锁的概念我们可以在维基百科上得到。下图给出了导致死锁的常见场景。在这篇博客中,我将分享如何使用JDK标准工具jstack来检测死锁情况。首先我们要编写一个Java程序,它将导致死锁:packagethread;publicclassDeadLockExample{/**线程1:锁定资源1线程2:锁定资源2*/publicstaticvoidmain(String[]args){finalStringresource1="ABAP";finalStringresource2="Java";//t1尝试锁定资源1,然后锁定资源2Threadt1=newThread(){publicvoidrun(){synchronized(resource1){System.out.println("Thread1:lockedresource1");尝试{Thread.sleep(100);}catch(Exceptione){}synchronized(resource2){System.out.println("Thread1:lockedresource2");}}}};Threadt2=newThread(){publicvoidrun(){synchronized(resource2){System.out.println("线程2:lockedresource2");try{Thread.sleep(100);}catch(Exceptione){}synchronized(resource1){System.out.println("Thread2:lockedresource1");}}}};t1.start();t2.start();}}执行这个程序,你会得到输出:Thread1:lockedresource1Thread2:lockedresource2然后使用命令jps-l-m列出这个死锁程序的进程id.在我的例子中是51476:输入jstack+processid,它会显示所有关于死锁的详细信息:这里对象0x00000000d6f64988和0x00000000d6f649b8代表两个资源字符串“ABAP”和“Java”。更新于2017-03-04Saturday10:35PMhowtogetthethreadstateofalong-runningapplications假设你发现了一个长时间运行的应用程序,它的CPU利用率很高,你想知道哪一行是相关的。使用下面的代码来模拟长时间运行情况:封装线程;导入java.util.ArrayList;importjava.util.List;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;classMyThreadimplementsRunnable{privateList
