Java Multithreading: "Base Piece" 09 Interrupt () and thread termination mode

Source: Internet
Author: User
Tags exit join sleep thread

1. Interrupt () Notes

Before describing how to terminate a thread, it is necessary to understand the interrupt () first.

For interrupt (), the Java DJK documentation is described as follows:

http://docs.oracle.com/javase/7/docs/api/

Interrupts this thread.

Unless the current thread is interrupting itself, which are always permitted, the CheckAccess method of this thread is INVO ked, which may cause a SecurityException to be thrown.

If this thread was blocked in a invocation of the wait (), wait (long), or wait (long, int) methods of the Object class, or O f the Join (), join (long), join (long, int), sleep (long), or sleep (long, int), methods's This class, then its interrupt STA Tus'll be cleared and it'll receive an interruptedexception.

If this thread was blocked in a I/O operation upon an interruptible channel then the channel'll be closed, the thread ' s Interrupt status would be set, and the thread would receive a closedbyinterruptexception.

If this thread was blocked in a Selector then the thread ' s interrupt status would be set and it would return immediately from The selection operation, possibly with a non-zero value, just as if the selector ' s wakeup method were invoked.

If none of the previous conditions hold then this thread ' s interrupt status would be set.

Interrupting a thread that isn't alive need not have any effect.

The General meaning is:

The effect of interrupt () is to interrupt this thread.

This thread interrupts itself to be allowed, and when other threads call this thread's interrupt () method, the permissions are checked by CheckAccess (). This may throw a SecurityException exception.

If this thread is in a blocking state: The call thread's Wait (), the Waiting (long), or (long, int) will let it enter the standby (blocking) state, or the calling thread's join (), join (long), join (long, int), Sleep ( Long), sleep (long, int) also lets it enter a blocking state. If the thread is blocking the state, its interrupt () method is invoked, and its "break state" is cleared and a Interruptedexception exception is received. For example, a thread enters a blocking state by a wait (), at which point the thread is interrupted by interrupt (), and the call to interrupt () immediately sets the thread's interrupt mark to "true", but as the thread is blocked, the interrupt mark is immediately cleared to "false" , at the same time, a Interruptedexception exception is generated.

If the thread is blocked in a selector selector, it is interrupted by interrupt (), the thread's interrupt token is set to true, and it is immediately returned from the selection operation.

If it does not fall into the previous scenario, its interrupt tag is set to "true" when the thread is disconnected through interrupt ().

Breaking a "terminated thread" does not produce any action.

2. How to terminate a thread

The Stop () and Suspend () methods in thread are not recommended for use because of inherent insecurity!

Next, I'll discuss the way threads terminate in "blocking state" and "run state" separately, and then summarize a common approach.

2.1 Terminating a thread in "blocked state"

Typically, we terminate a thread that is in the blocked state by means of "break".

When a thread enters a blocking state by means of a call to sleep (), a Wait (), join (), or the interrupt () of the calling thread to set the thread's interrupt token to true. Because of the blocking state, the interrupt mark is cleared and a interruptedexception exception is generated. You can terminate the thread by placing the interruptedexception in the appropriate form as follows:

@Override public
Void Run () {a
    try {while
        (true) {
            //Execute task ...
        }
    catch (Interruptedexception ie {  
        //due to a interruptedexception exception, exit while (true) loop, thread terminated!
    }
}

Description: The interrupt () of the calling thread produces interruptedexception interrupts while the thread is in a blocked state while the task is constantly executing in while (true). The interrupted capture is outside of the while (true), which exits the while (true) loop!

Note: The Interruptedexception capture is typically placed outside of the while (true) loop, so that the while (true) loop exits when an exception is generated. Otherwise, the interruptedexception in the while (true) loop body requires additional add-and-exit processing. The form is as follows:

@Override public
Void Run () {while
    (true) {
        try {
            //Execute task ...
        } catch (Interruptedexception IE) {
  
   //interruptedexception within a while (true) loop.
            //When the thread produces a interruptedexception exception, while (true) continues to run! Need to manually exit the break
            ;
        }}

  

Description: The above interruptedexception exception is captured within Whle (true). When a interruptedexception exception is generated, it is still in the while (true) loop outside of the catch processing, and an extra execution of a while (true) is required to exit the while (true) loop body.

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.