Interrupt, isinterrupt, interrupted of Java thread

Source: Internet
Author: User

From: http://jay-kid.iteye.com/blog/557064

In Java Network Programming, we can see an example that thread. Interrupt () is used to close the waiting thread. I don't quite understand, so I checked the Java API.

If you are interested, you can first look at the API content and then look at the following summary:

 

1. Both thread. isinterrupt () and thread. interrupted () return the status of the current thread interrupt.

  • Thread. isinterrupt () is a non-static function with the goal of "thread instance". Its general usage is as follows,
Java code  
  1. Testinterrupt T = new testinterrupt ();
  2. T. Start ();
  3. System. Out. println (T. isinterrupt ());

 

  • Thread. interrupted () is a static function with the goal of "current thread"
Java code  
  1. System. Out. println (thread. interrupted ());

In addition, it will "reset" the interrupt status of the current thread. If the isinterrupt status of the current thread is set to true, it will return true, but then the isinterrupt status will be reset to false. Call (thread) T. isinterrupt or thread. interrupted will return false

 

 

2. There are several possibilities after interrupt () is called:

 

When the thread is blocked:

  • If the thread is callingObjectClasswait(),wait(long)Orwait(long, int)Method, orjoin(),join(long),join(long, int),sleep(long)Orsleep(long, int)If the isinterrupt status is cleared and set to false, the isinterruptedexception is also received.
Java code  
  1. Public void run (){
  2. While (! Thread. currentthread (). isinterrupted ()){
  3. Try {
  4. Thread. Sleep (1000 );
  5. } Catch (interruptedexception IE ){
  6. Ie. printstacktrace ();
  7. }
  8. }
  9. }

As shown above, if the thread runs to the thread. when sleep (1000) is used, the interrupt () method of the thread is called by other threads. It will enter the catch section and then run while (! Thread. currentthread (). isinterrupted () Exits run and the thread is destroyed.

 

  • If the thread is blocked in I/O operations on the interrupted channel, the channel will be closed and the isinterrupt status of the thread will be set to true, and the thread will receiveClosedByInterruptException.

 

 

When the thread runs normally:

  • This thread is not affected and continues to run, but the isinterrupt status of this thread will be set to true
Java code  
  1. Public void run (){
  2. While (! Thread. currentthread (). isinterrupted ()){
  3. Try {
  4. // A: no blocking code .......
  5. ......
  6. // B: interrupt .......
  7. ......
  8. // C: no blocking code .......
  9. } Catch (interruptedexception IE ){
  10. Ie. printstacktrace ();
  11. }
  12. }
  13. }

As shown above, when the thread runs normally and is called interrupt () when it runs to point B, the thread continues to run normally, but the isinterrupt status is set to true, after completing the code for A, B, and C, go to the while (! Thread. currentthread (). isinterrupted (), the thread is destroyed. If the while check condition is changed to (true), the thread will not be affected and will continue to run.

Related Article

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.