死锁 - 检测 - 中断

发布时间:2019-11-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了死锁 - 检测 - 中断脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.sky.d_contracting_out;  import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; import java.lang.management.ThreadMXBean; import java.util.concurrent.locks.ReentrantLock;  /**  * 模拟出一个死锁现场,然后用中断来处理死锁  */ public class DeadLocKDEmo implements Runnable {          public static ReentrantLock l1 = new ReentrantLock();     public static ReentrantLock l2 = new ReentrantLock();          PRivate int lock;          public DeadLockDemo(int lock) {         this.lock = lock;     }      @override     public void run() {         try {             if(lock == 1){                 //普通的lock.lock()是不能响应中断的,lock.lockInterruptibly()能够响应中断                 l1.lockInterruptibly();                 Thread.sleep(500);                 l2.lockInterruptibly();             } else {                 l2.lockInterruptibly();                 Thread.sleep(500);                 l1.lockInterruptibly();             }                      } catch (Exception e) {         } finally {             if(l1.isHeldByCurrentThread()){                 l1.unlock();             }             if(l2.isHeldByCurrentThread()){                 l2.unlock();             }             System.out.println("线程退出");         }              }          public static void main(String[] args) throws InterruptedException {         Thread t1 = new Thread(new DeadLockDemo(1));         Thread t2 = new Thread(new DeadLockDemo(2));                  t1.start();         t2.start();                  Thread.sleep(1000);                  //启动:发生死锁,线程1得到l1,线程2得到l2,然后彼此又想获得对方的锁:jps jsatck命令查看                  //检查死锁线程并中断         //DeadlockChecker.check();     }          /** 检查死锁  */     static class DeadlockChecker {         private final static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();                  final static Runnable deadLockChecker = new Runnable() {                          @Override             public void run() {                 while(true){                     long[] deadLockedThreadIds = mbean.findDeadlockedThreads();                     if(deadLockedThreadIds != null){                         ThreadInfo[] threadInfos = mbean.getThreadInfo(deadLockedThreadIds);                         for(Thread t : Thread.getAllStackTraces().keySet()){                             for(ThreadInfo ti : threadInfos){                                 if(ti.getThreadId() == t.getId()){                                     t.interrupt();                                 }                             }                         }                     }                     try {                         Thread.sleep(5000);                     } catch (InterruptedException e) {                         e.printStackTrace();                     }                 }                              }         };                  public static void check() {             Thread t = new Thread(deadLockChecker);             t.setDaemon(true);             t.start();         }     }  } 
  • jps:查看当前运行的java线程

  • jstack:查看线程栈上的信息

死锁 - 检测 - 中断

脚本宝典总结

以上是脚本宝典为你收集整理的死锁 - 检测 - 中断全部内容,希望文章能够帮你解决死锁 - 检测 - 中断所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。