A detailed introduction to thread. Interrupt ()

Source: Internet
Author: User

In jdk1.0, you can use the stop method to terminate the termination. However, this method has been disabled now. Use the interrupt method instead.

The thread. Interrupt () method does not interrupt a running thread. It 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 state early.

The interrupt method does not force terminate a thread. It can only set the thread's interrupted status. The thread generally uses the following method:
While (! Thread. currentthread (). isinterrupted () & more work to do)

{...}

When the block thread (sleep () or join () is called interrupt, interruptexception is generated. whether to terminate the thread is determined by the thread itself. The general form of the program is:
Public void run ()
{
Try
{
...
While (! Thread. currentthread (). isinterrupted () & more work to do)
{
Do more work
}
}
Catch (interruptedexception E)
{
// Thread was interrupted during sleep or wait
}
Finally
{
Cleanup, if required
}
// Exiting the run method terminates the thread
}

The thread. Sleep method also produces interruptedexception. Therefore, if the sleep method is called every time you finish some work, you do not need to check isinterrupted, but directly capture interruptedexception.

Bytes ---------------------------------------------------------------------------------------

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?

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:
......
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!

We can use two methods to interrupt the Thread Through interrupt (). See the following example:
Java code
Class atask implements runnable {

Private double Ds = 0.0;

Public void run (){

Try {
// Check whether the program is interrupted
While (! Thread. interrupted ()){
System. Out. println ("I am running! ");
// Point1 before sleep
Thread. Sleep (20 );
// Point2 after sleep
System. Out. println ("calculating ");
For (INT I = 0; I <900000; I ++ ){
D = d + (math. PI + math. E)/d;
}
}

} Catch (interruptedexception e ){
System. Out. println ("exiting by exception ");
}

System. Out. println ("atask. Run () interrupted! ");
}
}

If an interruption occurs after point2 before point1, two different results are generated. You can modify the thread in interrupttasktest main. sleep () to interrupt before point1 or after point2.
If a thread is interrupted before point1, the program will call the thread. during Sleep (), interruptedexception is thrown to end the thread. this is the same as in thread. sleep () is interrupted with the same effect. the program running result may be as follows:
Java code
I am running!
Calculating
I am running!
Calculating
I am running!
Calculating
I am running!
****************************
Interrupted thread!
****************************
Exiting by exception
Atask. Run () interrupted!

If an interruption occurs after point2, the thread will continue to execute the next while to determine the interruption status. The program running result may be as follows:
Java code
I am running!
Calculating
I am running!
Calculating
I am running!
Calculating
****************************
Interrupted thread!
****************************
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.