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
- Class atask implements runnable {
- Private double Ds = 0.0;
- Public void run (){
- // Print "I am running! "And time-consuming floating point computing
- While (true ){
- System. Out. println ("I am running! ");
- For (INT I = 0; I <900000; I ++ ){
- D = d + (math. PI + math. E)/d;
- }
- // Signal that the thread scheduler can switch to other processes
- Thread. Yield ();
- }
- }
- }
- Public class interrupttasktest {
- Public static void main (string [] ARGs) throws exception {
- // Submit the task to a thread for execution
- Thread t = new thread (New atask ());
- T. Start ();
- // Run the thread with a disconnection time interrupted
- Thread. Sleep (100 );
- System. Out. println ("****************************");
- System. Out. println ("interrupted thread! ");
- System. Out. println ("****************************");
- T. Interrupt ();
- }
- }
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
- ......
- I am running!
- I am running!
- I am running!
- I am running!
- ****************************
- Interrupted thread!
- ****************************
- I am running!
- I am running!
- I am running!
- I am running!
- I am running!
- ....
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
- Class atask implements runnable {
- Private double Ds = 0.0;
- Public void run (){
- // Print "I am running! "And time-consuming floating point computing
- Try {
- While (true ){
- System. Out. println ("I am running! ");
- For (INT I = 0; I <900000; I ++ ){
- D = d + (math. PI + math. E)/d;
- }
- // The sleep duration. interruptedexception will be thrown during interruption.
- Thread. Sleep (50 );
- }
- } Catch (interruptedexception e ){
- System. Out. println ("atask. Run () interrupted! ");
- }
- }
- }
The program running result is as follows:
Java code
- I am running!
- I am running!
- ****************************
- Interrupted thread!
- ****************************
- 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
- Class atask implements runnable {
- Private double Ds = 0.0;
- Public void run (){
- // Check whether the program is interrupted
- While (! Thread. interrupted ()){
- System. Out. println ("I am running! ");
- For (INT I = 0; I <900000; I ++ ){
- D = d + (math. PI + math. E)/d;
- }
- }
- System. Out. println ("atask. Run () interrupted! ");
- }
- }
The program running result is as follows:
Java code
- I am running!
- I am running!
- I am running!
- I am running!
- I am running!
- I am running!
- I am running!
- ****************************
- Interrupted thread!
- ****************************
- Atask. Run () interrupted!