Java死锁检测

ThreadMXBean

常用方法:

  /** ThreadMXBean常用方法test */
        ThreadMXBean threadMXBean= ManagementFactory.getThreadMXBean();
        int count=threadMXBean.getThreadCount();//获取当前线程数目
        System.out.println("当前线程数为:"+count);
        long[]  threadIds=threadMXBean.getAllThreadIds();
        System.out.println("当前线程id们为:"+ JSONObject.toJSONString(threadIds));
        ThreadInfo[] threadInfos=threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds());
        System.out.println("当前线程的信息:"+JSONObject.toJSONString(threadInfos));
        System.out.println("是否支持测量线程执行时间:"+threadMXBean.isCurrentThreadCpuTimeSupported());
/**
 * 死锁检测工具
 * Created by liuhuichao on 2017/6/21.
 */
public class DeadLockCheckChecker {

    private  final static ThreadMXBean mbean=ManagementFactory.getThreadMXBean();
    final static Runnable deadLockCheck=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(int i=0;i<threadInfos.length;i++){
                            if(t.getId()==threadInfos[i].getThreadId()){
                                t.interrupt();
                            }
                        }
                    }
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }

        }
    };


    public static void check(){
        Thread thread=new Thread(deadLockCheck);
        thread.setDaemon(true);
        thread.start();
    }

}

可中断性测试:

/**ReentrantLock可中断性测试
 * Created by liuhuichao on 2017/6/20.
 */
public class ReentrantLockInterruptedTest implements Runnable {

    public static ReentrantLock lock1=new ReentrantLock();
    public static ReentrantLock lock2=new ReentrantLock();
    int lock;

    /**
     * 控制加锁顺序,方便构造死锁
     * @param lock
     */
    public ReentrantLockInterruptedTest(int lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        try{
            if(lock==1){
                lock1.lockInterruptibly();
                try{
                    Thread.sleep(500);
                }catch (InterruptedException e){}
                lock2.lockInterruptibly();
            }else{
                lock2.lockInterruptibly();
                try{
                    Thread.sleep(500);
                }catch (InterruptedException e){}
                lock1.lockInterruptibly();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            if(lock1.isHeldByCurrentThread()){
                lock1.unlock();
            }
            if(lock2.isHeldByCurrentThread()){
                lock2.unlock();
            }
            System.out.println("线程退出:"+Thread.currentThread().getId());
        }

    }

    public static void main(String[] args)  throws Exception{
        ReentrantLockInterruptedTest l1=new ReentrantLockInterruptedTest(1);
        ReentrantLockInterruptedTest l2=new ReentrantLockInterruptedTest(2);
        Thread t1=new Thread(l1);
        Thread t2=new Thread(l2);
        t1.start();
        t2.start();
        Thread.sleep(1000);
       DeadLockCheckChecker.check();

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值