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,创建线程处理任务,

  • 相关阅读:
    从太空到地球某个位置的轨迹录像制作 | Earth Zoom in/out Tutorial (Record Video)
    DNA replication timing数据库
    探索ENCODE数据库 | Encyclopedia of DNA Elements
    第100天: 三木板模型算法项目实战
    第99天:UDP 编程
    第98天:图像库 PIL 实例—验证码去噪
    第97天:图像库 PIL(二)
    第96天:图像库 PIL(一)
    第95天:StringIO & BytesIO
    第94天:数据分析之 pandas 初步
  • 原文地址:https://www.cnblogs.com/balaamwe/p/2318934.html
Copyright © 2011-2022 走看看