Java - threading issue

My question is related to all of these methods (including Thread.sleep(...) ) that throw InterruptedException .

I found a Sun manual that says

InterruptedException is an exception when sleep throws when another thread interrupts the current thread while the sleep asset is.

Does this mean that the interrupt will be ignored if sleep not active during the interrupt?

Suppose I have two threads: threadOne and threadTwo . threadOne creates and starts threadTwo . threadTwo executes runnable, whose start method looks something like this:

 public void run() { : : try { Thread.sleep(10 * 1000); } catch (InterruptedException e) { return; } : : : // In the middle of two sleep invocations : : try { Thread.sleep(10 * 1000); } catch (InterruptedException e) { return; } : : } 

After creating the thread, threadOne aborts threadTwo . Suppose threadTwo is in the middle of two sleep calls during an interrupt (when the sleep method was not used), then the second sleep method will throw an InterrupteException as soon as it is called?

If not, will this interrupt be ignored forever?

How to be sure that threadTwo will always know about the interrupt (it doesn’t matter if one of its sleep methods is active)?

+4
source share
4 answers

In Sun Windows JDK, the thread will actually throw an InterruptedException when you enter sleep() :

 public static final void main(String args[]) throws Exception { final Thread main = Thread.currentThread(); Thread t = new Thread() { @Override public void run() { main.interrupt(); } }; t.start(); t.join(); Thread.sleep(1000); System.out.println("Not interrupted!"); } 

The sleep() API documentation can be interpreted as meaning that this is a required behavior:

throws InterruptedException - if there is a thread, the current thread has interrupted. The interrupted status of the current thread is cleared when this exception occurs.

But this is not very clear, so I will not depend on it and instead check isInterrupted() manually.

+2
source

From javadoc:

If this thread is blocked in a call to wait (), wait (long), or wait (long, int) methods, an object class or join (), join (long), join (long, int), sleep (long) or sleep (long , int) methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

If this thread is blocked in an I / O operation on an intermittent channel, the channel will be closed, the state of interruption of the stream will be set, and the thread will receive a ClosedByInterruptException.

If this thread is blocked in the Selector, then the interruption of the status of the stream will be set, and it will return immediately after selection, possibly with a non-zero value, as if the selector was called by the wake-up method.

If none of the previous conditions hold, this interrupt flow status will be set.

This means that you must check the interrupted status to make sure your thread knows about the interrupt. There are two ways to do this: isInterrupted () and interrupted (). The latter clears the interrupted status.

Something like that:

 while(!Thread.interrupted()) { ... try { Thread.sleep(10 * 1000); } catch (InterruptedException e) { return; } } 
+3
source

The Java documentation is a bit misleading. If the interrupted status of the thread is set, calling sleep() on that thread will immediately throw an InterruptException .

This applies even if the thread was interrupted before sleep() was called.

As stated above, you can also check with Thread.isInterrupted() if you want to handle interrupts yourself.

+2
source

Interruption of interruption is of interest only during sleep flow. This will not be called by later sleep () if the thread has been mixed up somewhere earlier. Only interception during sleep () matters because it accurately breaks the sleep () call.

-1
source

Source: https://habr.com/ru/post/1308640/


All Articles