- Private void checkdeadlock (){
- // Only read/write/connect/Write future can cause dead lock.
- If (! (This instanceof closefuture | this instanceof writefuture |
- This instanceof readfuture | this instanceof connectfuture )){
- Return;
- }
- // Get the current thread stacktrace.
- // Using thread. currentthread (). getstacktrace () is the best solution,
- // Even if slightly less efficient than doing a new exception (). getstacktrace (),
- // As internally, it does exactly the same thing. The advantage of using
- // This solution is that we may benefit some improvement with some
- // Future versions of Java.
- Stacktraceelement [] stacktrace = thread. currentthread (). getstacktrace ();
- // Simple and quick check.
- For (stacktraceelement S: stacktrace ){
- If (abstractpollingioprocessor. Class. getname (). Equals (S. getclassname ())){
- Illegalstateexception E = new illegalstateexception ("T ");
- E. getstacktrace ();
- Throw new illegalstateexception (
- "Dead Lock:" + iofuture. Class. getsimplename () +
- ". Await () was invoked from an I/O processor thread." +
- "Please use" + iofuturelistener. Class. getsimplename () +
- "Or configure a proper thread model alternatively .");
- }
- }
- // And then more precisely.
- For (stacktraceelement S: stacktrace ){
- Try {
- Class <?> CLS = defaultiofuture. Class. getclassloader (). loadclass (S. getclassname ());
- If (ioprocessor. Class. isassignablefrom (CLS )){
- Throw new illegalstateexception (
- "Dead Lock:" + iofuture. Class. getsimplename () +
- ". Await () was invoked from an I/O processor thread." +
- "Please use" + iofuturelistener. Class. getsimplename () +
- "Or configure a proper thread model alternatively .");
- }
- } Catch (exception cnfe ){
- // Ignore
- }
- }
- }
The deadlock detection algorithm is located in defaultiofuture. The strategy is to split the Deadlock Detection Algorithm into two steps. First, it uses a simple and fast method for detection, and then uses a more precise method for detection. The first method obtains the element array of the entire stack of the current thread through getstacktrace, and then traverses this array to check whether the class called by a certain layer is abstractpollingioprocessor. If yes, there is a loop call and a deadlock occurs. This is because abstractpollingioprocessor is an abstract class that implements ioprocessor. It contains an executor and creates a thread to process tasks,