Use of the Thread interrupt () method __thread

Source: Internet
Author: User

Interrupt () only changes the state of interruption:
Interrupt () does not break a running thread. What this method actually does is to throw an interrupt signal when the thread is blocked, so that the threads can exit the blocked state. More precisely, if the thread is object.wait, One of the three methods of Thread.Join and Thread.Sleep is blocked, it will receive an interrupt exception (interruptedexception), thus terminating the blocked state prematurely. If the thread is not blocked, then call interrupt () will not work; just set the interrupt flag bit to True


Also note the difference between the following three methods:

Publicvoid Interrupt ()

public static Boolean Interrupted (): The interrupt state of a thread is cleared by this method

public boolean isinterrupted (): Tests whether the thread has been interrupted. The interrupt state of a thread is not affected by this method.

Summarize:

T.interrupt () does not break the executing thread, but sets the thread's flag bit to true. However, if the thread is interrupted by the call to sleep (), join (), the Wait () method, the methods throw interruptedexception, and when this exception is caught in the catch block, the thread's interrupt flag bit has been set to false. So call t.isinterrupted () in this catch block, thread.interrupted () is always false,

The difference between t.isinterrupted and thread.interrupted () is obvious in the API, thread.interrupted () If the current interrupt flag is true, the interrupt flag bit will be set to false after the adjustment is completed. And t.isinterrupted doesn't do anything with the flag bit.



If we have a task as follows, give it to a Java thread to execute, how can we guarantee the call to interrupt () to interrupt it?   Java code class atask implements runnable{           Private double d = 0.0;                public void run () {            //dead Loop perform print "I am running!" and do time consuming floating-point calculations             while  (True) {                 System.out.println ("I am running!");                                 for  (Int i = 

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.