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

原文地址:https://www.cnblogs.com/balaamwe/p/2318934.html