Deadlock Detection in Mina

Source: Internet
Author: User
  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. // Get the current thread stacktrace.
  8. // Using thread. currentthread (). getstacktrace () is the best solution,
  9. // Even if slightly less efficient than doing a new exception (). getstacktrace (),
  10. // As internally, it does exactly the same thing. The advantage of using
  11. // This solution is that we may benefit some improvement with some
  12. // Future versions of Java.
  13. Stacktraceelement [] stacktrace = thread. currentthread (). getstacktrace ();
  14. // Simple and quick check.
  15. For (stacktraceelement S: stacktrace ){
  16. If (abstractpollingioprocessor. Class. getname (). Equals (S. getclassname ())){
  17. Illegalstateexception E = new illegalstateexception ("T ");
  18. E. getstacktrace ();
  19. Throw new illegalstateexception (
  20. "Dead Lock:" + iofuture. Class. getsimplename () +
  21. ". Await () was invoked from an I/O processor thread." +
  22. "Please use" + iofuturelistener. Class. getsimplename () +
  23. "Or configure a proper thread model alternatively .");
  24. }
  25. }
  26. // And then more precisely.
  27. For (stacktraceelement S: stacktrace ){
  28. Try {
  29. Class <?> CLS = defaultiofuture. Class. getclassloader (). loadclass (S. getclassname ());
  30. If (ioprocessor. Class. isassignablefrom (CLS )){
  31. Throw new illegalstateexception (
  32. "Dead Lock:" + iofuture. Class. getsimplename () +
  33. ". Await () was invoked from an I/O processor thread." +
  34. "Please use" + iofuturelistener. Class. getsimplename () +
  35. "Or configure a proper thread model alternatively .");
  36. }
  37. } Catch (exception cnfe ){
  38. // Ignore
  39. }
  40. }
  41. }

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,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.