java - Thread is interrupted by calling interrupt(), but Thread.isInterrupted() returns false -


I am testing the interrupted exception with the following test code:

  Runnabal Runman = New Runnabal () {@Override Public runs zero () (for (int i = 0; i & lt; 6; i ++) {System.out.println ("i =" + i); if (i == 3) {System.out.println ("I == 3, Thread =" + Thread.wrenchseed (). GetId ()); // I invoke interrupt (at work thread) Thread.currentThread () In the middle (). Try {Thread.currentThread (). Join ();} Grip (Interrupted ejection e) {// I took the interruption lifted expression System.out.printLN (Thread.currentThread (). GetId () + "is interrupted!"); Thread.currentThread () between ().;}}}}}; Thread working thread = new thread (runema); Try workingThread.start (); {WorkThread.join ();} Grip (Interrupted eXpression E) {// SELLO} // The following logs show me the work Thread. This interrupted () is wrong, why? System.out.println ("working thread" (interrupted + "workThread.getId (+)")? "+ WorkThread.isInterrupted ());  Run ,  interrupt ()  current task thread, and a  paused exception Caught.  

In the main thread, the code of my last line is a System.out.println (...) which prints the interrupt state of the working thread. Since I have taken the interrupted step in run , though I should get the message that workingThread.isInterrupted () true , But I got a liar, why?

Oracle says (my emphasis has been added):

< Blockquote>

Interrupt status flag

The interrupt state is implemented using an internal flag which is known as the interrupt state. This flag is set by including Thread.interrupt. When a thread determines the static method for an interrupted thread, then the position is cleared in between. The non-static blocking method, which is used by a thread to query the interrupt statement of another, does not change the interrupt status flag.

According to the conference, Any method that emerges from throwing an obstruction exception is when it does so removes the situation in between However, it is always possible that in the middle The status will be set again, again restated if it is interrupted by another thread.

What you are seeing is that the thread is interrupted, which comes out of the law, it is included (actually join the object and implement it. Waiting, so I hope the Stacktrace will show that the wait is a way to throw exceptions), and the flag like the tricotrile will be cleared.

Depending on the handler of exceptions to determine whether the interrupt status flag of the thread should be reset again or not. Unless the handler is coded to separate something, the default estimate is that the exception was handled and there is no need to set the flag anymore.

is compatible with:

If this thread is blocked (wait) in a commute (wait), wait (long), or wait (long, integer ) After applying the object classes or joining (), join (long), (long, integer), sleep (long), or sleep (long, int), in this class methods, clear its interval state And it will take an interrupted step

I have an answer that looks like this Was designed in such a way.


Comments

Popular posts from this blog

c# - SignalR: "Protocol error: Unknown transport." when navigating to hub -

android - how to get distance of 2 beacons and make a condtion base on that distance -

c# - WPF Expander overlay used in ItemsControl -