interrupt,isinterrupt,interrupted Methods in Java threads

Source: Internet
Author: User

There are three methods in the threading thread class of Java, which is easy to confuse, explain here
(1) Interrupt: Interrupt state of the thread
(2) Isinterrupt: Thread is interrupted

(3) Interrupted: Returns the last interrupt state of the thread and clears the interrupt state

As an example:

[Java]View Plaincopy
  1. Usage:
  2. class MyThread extends Thread {
  3. ......
  4. ......
  5. Public void run () {
  6. Try  {  
  7. while (! Thread.CurrentThread (). isinterrupted ()) {
  8. //When the queue capacity is reached, it is blocked here   
  9. The inside of the//put calls Locksupport.park (), which is the method used to block the thread.   
  10. //When other threads call this thread's interrupt () method, an interrupt flag is set   
  11. //locksupport.part () detects this interrupt flag, throws Interruptedexception, and clears the thread's interrupt flag   
  12. ///So call Thread.CurrentThread () in the exception segment. Isinterrupted () returns to false   
  13. Arrayblockingqueue.put (somevalue);
  14. }
  15. } catch (interruptedexception e) {
  16. ///due to blocking library functions, such as: Object.wait,thread.sleep also clears the thread break state in addition to throwing exceptions, so it is possible to keep the interrupt state of the thread here   
  17. Thread.CurrentThread (). interrupt ();
  18. }
  19. }
  20. Public void cancel () {
  21. Interrupt ();
  22. }
  23. }
  24. External calls
  25. MyThread thread = new MyThread ();
  26. Thread.Start ();
  27. ......
  28. Thread.cancel ();
  29. Thread.isinterrupted ();


In general, blocking functions, such as: Thread.Sleep, Thread.Join, object.wait, Locksupport.park, such as when checking the interrupt state of a thread, throws interruptedexception and clears the interrupt state of the thread.

There are two possible scenarios for interruptedexception processing:
(1) The outer code can handle this exception and throw the exception directly.
(2) If the exception cannot be thrown, such as in the Run () method, because the interrupt state of the thread is cleared when the exception is obtained, and the interrupt state of the thread needs to be preserved, call Thread.CurrentThread (). Interrupt ()

In addition, thread.interrupted () in the JDK Library source code is more commonly used, because it can get the interrupt flag value of the last thread, but also can clear the interrupt flag of the thread, double benefit, but there are disadvantages, that is, this function has the side effect of clearing the interrupt state, not easy to understand

interrupt,isinterrupt,interrupted Methods in Java threads

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.