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 =