• 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. }  
  • 相关阅读:
    NPOI操作 EXCEL文件
    mysql 临时变量,临时表
    ReSharper制作Template帮助我们快速输入代码
    临时表的实际运用
    如何避免项目重复循环引用
    mysql 5.1以上版本数据库 使用定时器功能(Event)
    Fedora12下装SMPlayer
    Fedora12启动时激活网卡并将firefox不设置为脱机工作
    设置yum
    Fedora 有用更新源上海交大的yum源
  • 原文地址:https://www.cnblogs.com/jiangye/p/3319148.html
Copyright © 2020-2023  润新知