java 线程死锁的检测


例子程序: 
Java代码  收藏代码
  1. import java.util.concurrent.CountDownLatch;  
  2. import java.util.concurrent.ExecutorService;  
  3. import java.util.concurrent.Executors;  
  4.   
  5. /** 
  6.  * Hello world! 
  7.  */  
  8. public class App {  
  9.     public static void main(String[] args) throws InterruptedException {  
  10.         System.out.println("Hello World!");  
  11.         ExecutorService executorService = Executors.newFixedThreadPool(2);  
  12.         byte[] i = new byte[0];  
  13.         byte[] j = new byte[0];  
  14.         final CountDownLatch countDownLatch = new CountDownLatch(2);  
  15.         executorService.execute(new DeadThread1(i, j,countDownLatch));  
  16.         executorService.execute(new DeadThread2(i, j,countDownLatch));  
  17.         countDownLatch.await();  
  18.         System.out.println("done !!!");  
  19.     }  
  20.   
  21.     public static class DeadThread1 implements Runnable {  
  22.   
  23.         private byte[] i;  
  24.         private byte[] j;  
  25.         private CountDownLatch countDownLatch;  
  26.   
  27.         public DeadThread1(byte[] i, byte[] j, CountDownLatch countDownLatch) {  
  28.             this.i = i;  
  29.             this.j = j;  
  30.             this.countDownLatch = countDownLatch;  
  31.         }  
  32.   
  33.   
  34.         @Override  
  35.         public void run() {  
  36.             synchronized (i) {  
  37.                 try {  
  38.                     Thread.sleep(1000);  
  39.                 } catch (InterruptedException e) {  
  40.                     e.printStackTrace();    
  41.                 }  
  42.                 synchronized (j) {  
  43.                     System.out.println(Thread.currentThread().getName() + "is running!!");  
  44.                     countDownLatch.countDown();  
  45.                 }  
  46.             }  
  47.         }  
  48.     }  
  49.   
  50.     public static class DeadThread2 implements Runnable {  
  51.   
  52.         private byte[] i;  
  53.         private byte[] j;  
  54.         private CountDownLatch countDownLatch;  
  55.   
  56.         public DeadThread2(byte[] i, byte[] j, CountDownLatch countDownLatch) {  
  57.             this.i = i;  
  58.             this.j = j;  
  59.             this.countDownLatch = countDownLatch;  
  60.         }  
  61.   
  62.   
  63.         @Override  
  64.         public void run() {  
  65.             synchronized (j) {  
  66.                 try {  
  67.                     Thread.sleep(1000);  
  68.                 } catch (InterruptedException e) {  
  69.                     e.printStackTrace();    
  70.                 }  
  71.                 synchronized (i) {  
  72.                     System.out.println(Thread.currentThread().getName() + "is running!!");  
  73.                     countDownLatch.countDown();  
  74.                 }  
  75.             }  
  76.         }  
  77.     }  
  78. }  


通过jps找到当前进程号: 
Shell代码  收藏代码
  1. guohaozhao116008@GUOHAOZHAO11600 /d  
  2. $ jps  
  3. 7448 RemoteMavenServer  
  4. 6600 JConsole  
  5. 6340 Jps  
  6. 6272  
  7. 7268 AppMain  

通过jstack查看堆栈信息: 
Shell代码  收藏代码
  1. guohaozhao116008@GUOHAOZHAO11600 /d  
  2. $ jstack  
  3. Usage:  
  4.     jstack [-l] <pid>  
  5.         (to connect to running process)  
  6.     jstack -F [-m] [-l] <pid>  
  7.         (to connect to a hung process)  
  8.     jstack [-m] [-l] <executable> <core>  
  9.         (to connect to a core file)  
  10.     jstack [-m] [-l] [server_id@]<remote server IP or hostname>  
  11.         (to connect to a remote debug server)  
  12.   
  13. Options:  
  14.     -F  to force a thread dump. Use when jstack <pid> does not respond (process is hung)  
  15.     -m  to print both java and native frames (mixed mode)  
  16.     -l  long listing. Prints additional information about locks  
  17.     -h or -help to print this help message  
  18.   
  19. guohaozhao116008@GUOHAOZHAO11600 /d  
  20. $ jps  
  21. 7448 RemoteMavenServer  
  22. 6600 JConsole  
  23. 6340 Jps  
  24. 6272  
  25. 7268 AppMain  
  26.   
  27. guohaozhao116008@GUOHAOZHAO11600 /d  
  28. $ jstack -l 7268  
  29. 2013-05-30 18:36:41  
  30. Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.14-b01 mixed mode):  
  31.   
  32. Found one Java-level deadlock:  
  33. =============================  
  34. "pool-1-thread-2":  
  35.   waiting to lock monitor 0x000000000677c208 (object 0x00000000eafc3e18, a [B),  
  36.   which is held by "pool-1-thread-1"  
  37. "pool-1-thread-1":  
  38.   waiting to lock monitor 0x0000000006771be8 (object 0x00000000eafc3e28, a [B),  
  39.   which is held by "pool-1-thread-2"  
  40.   
  41. Java stack information for the threads listed above:  
  42. ===================================================  
  43. "pool-1-thread-2":  
  44.         at com.sohu.suc.App$DeadThread2.run(App.java:74)  
  45.         - waiting to lock <0x00000000eafc3e18> (a [B)  
  46.         - locked <0x00000000eafc3e28> (a [B)  
  47.         at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)  
  48.         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)  
  49.         at java.lang.Thread.run(Thread.java:662)  
  50. "pool-1-thread-1":  
  51.         at com.sohu.suc.App$DeadThread1.run(App.java:45)  
  52.         - waiting to lock <0x00000000eafc3e28> (a [B)  
  53.         - locked <0x00000000eafc3e18> (a [B)  
  54.         at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)  
  55.         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)  
  56.         at java.lang.Thread.run(Thread.java:662)  
  57.   
  58. Found 1 deadlock.  


可以看到: 

这里发生了死锁。 

使用图形工具 jconsole 同样可以检测到: 

posted @ 2016-08-16 11:19  吃饭了吗  阅读(532)  评论(0编辑  收藏  举报