Use of thread interrupt () method

Source: Internet
Author: User

 

Address: http://blog.csdn.net/biangren/article/details/7522583

Interrupt () only changes the interrupt status:
Interrupt () does not interrupt a running thread. In fact, this method throws an interrupt signal when the thread is blocked, so that the thread can exit the blocking state. More specifically, if the thread is. wait, thread. join and thread. if sleep is blocked by one of the three methods, it will receive an interrupt exception (interruptedexception) to terminate the blocked status early. if the thread is not blocked, the call to interrupt () will not work. It is only to set the interrupt flag to true.

 

Note the differences between the following three methods:

Publicvoid interrupt ()

Public static Boolean interrupted (): This method clears the thread interruption status.

Public Boolean isinterrupted (): test whether the thread has been interrupted. The thread interruption status is not affected by this method.

Summary:

T. Interrupt () does not interrupt the thread being executed, but sets the flag of the thread to true. However, if the thread is interrupted when the sleep (), join (), and wait () methods are called, these methods throw interruptedexception. When this exception is caught in the catch block, the thread interrupt flag has been set to false, so t. isinterrupted (), thread. interrupted () is always false,

T. isinterrupted and thread. the difference between interrupted () is that the API has clearly stated that thread. interrupted () if the current interrupt flag is true, the interrupt flag is set to false after the call, and T. isinterrupted does not process the flag bit.

 

 

If we have a job as follows and hand it over to a Java thread for execution, how can we ensure that interrupt () is called to interrupt it?

Java code
  1. Class atask implements runnable {
  2. Private double Ds = 0.0;
  3. Public void run (){
  4. // Print "I am running! "And time-consuming floating point computing
  5. While (true ){
  6. System. Out. println ("I am running! ");
  7. For (INT I = 0; I <900000; I ++ ){
  8. D = d + (math. PI + math. E)/d;
  9. }
  10. // Signal that the thread scheduler can switch to other processes
  11. Thread. Yield ();
  12. }
  13. }
  14. }
  15. Public class interrupttasktest {
  16. Public static void main (string [] ARGs) throws exception {
  17. // Submit the task to a thread for execution
  18. Thread t = new thread (New atask ());
  19. T. Start ();
  20. // Run the thread with a disconnection time interrupted
  21. Thread. Sleep (100 );
  22. System. Out. println ("****************************");
  23. System. Out. println ("interrupted thread! ");
  24. System. Out. println ("****************************");
  25. T. Interrupt ();
  26. }
  27. }



After running this program, we find that the program is still running after interrupt () is called. If it is not forced to end, the program will continue to run as follows:

Java code
  1. ......
  2. I am running!
  3. I am running!
  4. I am running!
  5. I am running!
  6. ****************************
  7. Interrupted thread!
  8. ****************************
  9. I am running!
  10. I am running!
  11. I am running!
  12. I am running!
  13. I am running!
  14. ....


Although the thread is interrupted, there are two common methods to exit the thread:
Interruptedexception is thrown and thread. interrupted () is used to check whether an interrupt occurs. The following two methods are used:
1. in blocking operations, such as thread. when sleep () is interrupted, interruptedexception will be thrown. (Note: When an I/O operation cannot be interrupted, blocking and the Synchronized Method of the object is called to obtain the lock of the object, interruptedexception will not be thrown)

Java code
  1. Class atask implements runnable {
  2. Private double Ds = 0.0;
  3. Public void run (){
  4. // Print "I am running! "And time-consuming floating point computing
  5. Try {
  6. While (true ){
  7. System. Out. println ("I am running! ");
  8. For (INT I = 0; I <900000; I ++ ){
  9. D = d + (math. PI + math. E)/d;
  10. }
  11. // The sleep duration. interruptedexception will be thrown during interruption.
  12. Thread. Sleep (50 );
  13. }
  14. } Catch (interruptedexception e ){
  15. System. Out. println ("atask. Run () interrupted! ");
  16. }
  17. }
  18. }


The program running result is as follows:

Java code
  1. I am running!
  2. I am running!
  3. ****************************
  4. Interrupted thread!
  5. ****************************
  6. Atask. Run () interrupted!


You can see that when the task is interrupted, the task throws interruptedexception to exit the task.

2. thread. interrupted () checks for interruptions. thread. interrupted () can tell you whether the thread is interrupted, and will clear the interrupt status mark, so the program will not notify you twice that the thread is interrupted.

Java code
  1. Class atask implements runnable {
  2. Private double Ds = 0.0;
  3. Public void run (){
  4. // Check whether the program is interrupted
  5. While (! Thread. interrupted ()){
  6. System. Out. println ("I am running! ");
  7. For (INT I = 0; I <900000; I ++ ){
  8. D = d + (math. PI + math. E)/d;
  9. }
  10. }
  11. System. Out. println ("atask. Run () interrupted! ");
  12. }
  13. }


The program running result is as follows:

Java code
    1. I am running!
    2. I am running!
    3. I am running!
    4. I am running!
    5. I am running!
    6. I am running!
    7. I am running!
    8. ****************************
    9. Interrupted thread!
    10. ****************************
    11. Atask. Run () interrupted!

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.