java 线程死锁的检测

  1. import java.util.concurrent.CountDownLatch;  
  2. import java.util.concurrent.ExecutorService;  
  3. import java.util.concurrent.Executors;  
  4.   将夜www.jiangyea.com
  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. }  
posted @ 2013-09-13 14:31  将夜  阅读(352)  评论(0编辑  收藏  举报