zoukankan      html  css  js  c++  java
  • mina里的死锁检测

     
    1. private void checkDeadLock() {  
    2.        // Only read / write / connect / write future can cause dead lock.   
    3.        if (!(this instanceof CloseFuture || this instanceof WriteFuture ||  
    4.              this instanceof ReadFuture || this instanceof ConnectFuture)) {  
    5.            return;  
    6.        }  
    7.          
    8.        // Get the current thread stackTrace.   
    9.        // Using Thread.currentThread().getStackTrace() is the best solution,  
    10.        // even if slightly less efficient than doing a new Exception().getStackTrace(),  
    11.        // as internally, it does exactly the same thing. The advantage of using  
    12.        // this solution is that we may benefit some improvement with some  
    13.        // future versions of Java.  
    14.        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();  
    15.   
    16.        // Simple and quick check.  
    17.        for (StackTraceElement s: stackTrace) {  
    18.            if (AbstractPollingIoProcessor.class.getName().equals(s.getClassName())) {  
    19.                IllegalStateException e = new IllegalStateException( "t" );  
    20.                e.getStackTrace();  
    21.                throw new IllegalStateException(  
    22.                    "DEAD LOCK: " + IoFuture.class.getSimpleName() +  
    23.                    ".await() was invoked from an I/O processor thread.  " +  
    24.                    "Please use " + IoFutureListener.class.getSimpleName() +  
    25.                    " or configure a proper thread model alternatively.");  
    26.            }  
    27.        }  
    28.   
    29.        // And then more precisely.  
    30.        for (StackTraceElement s: stackTrace) {  
    31.            try {  
    32.                Class<?> cls = DefaultIoFuture.class.getClassLoader().loadClass(s.getClassName());  
    33.                if (IoProcessor.class.isAssignableFrom(cls)) {  
    34.                    throw new IllegalStateException(  
    35.                        "DEAD LOCK: " + IoFuture.class.getSimpleName() +  
    36.                        ".await() was invoked from an I/O processor thread.  " +  
    37.                        "Please use " + IoFutureListener.class.getSimpleName() +  
    38.                        " or configure a proper thread model alternatively.");  
    39.                }  
    40.            } catch (Exception cnfe) {  
    41.                // Ignore  
    42.            }  
    43.        }  
    44.    }  

      这个死锁检测算法位于DefaultIoFuture中,策略是分两步进行,首先是用一个简单而快速的方法检测,然后再用一个更精确地方法来检测。 第一个方法通过getStackTrace获得当前的线程的整个堆栈的元素数组,然后遍历这个数组,看是否有某一层的调用的class是AbstractPollingIoProcessor,如果有,就是存在循环调用,出现死锁。这是因为AbstractPollingIoProcessor是实现IoProcessor的抽象类,包含一个Executor,创建线程处理任务,

  • 相关阅读:
    国标ikecin插座资料
    LED平面管测试资料
    elementui使用MessageBox 弹框自定义内容$msgbox:实现一个textarea文本输入框
    ajax请求的时候后台有三个服务器地址
    前端用xshell向后端服务器部署项目
    vue+echarts 实现map3D地图tooltip弹框读取后台返回的数据,并显示弹框
    AWS网络架构及知识概述
    K8s控制器 StatefulSet
    pod的数据持久化2 NFS
    Pod的数据持久化1 hostPath 和emptyDir
  • 原文地址:https://www.cnblogs.com/balaamwe/p/2318934.html
Copyright © 2011-2022 走看看