0

I am not clear regarding these two methods from the perspective of setting the status of the thread.

Java Docs say that Thread.interrupt() sets the threads interrupt status flag and calling the Thread.interrupted() method gives the status of the thread and clears the flag.

When this comes to use in real scenarios..??

amj
  • 367
  • 1
  • 8
  • 26

2 Answers2

7

When some other thread calls Thread.interrupt() the method sets Thread's interrupt status flag (initially false) to true. If the Thread is in blocking method like Thread.sleep(), Thread.join() or Object.wait() it unblocks and throws an InterruptedException.

Thread.interrupted() is a static method which can be use to check the current value of interrupt status flag, true or false. It also clears the interrupt status , setting the flag to false. That is calling it twice in a row may likely return false the second time even if it returned true the first time (unless the thread were interrupted again, setting the interrupt status flag to true after the first call)

Note a third method Thread.isInterrupted() which can check the interrupt status without resetting.

Typical use cases:

  1. Break exceptionally from a blocking operation
  2. Determine if it is desired to continue a long sequence of instructions at some logical save/stop point
  3. Determine if it is desired to continue a sequence of instructions prior to beginning a long running task
  4. Stop executing an iterative process that would otherwise continue into perpetuity (while(true) is bad, while(!Thread.interrupted()) is better)
clinton
  • 612
  • 3
  • 6
  • 1
    Calling it twice in a row *may* return false even if it returned true the first time. But it may return true twice in a row, because it can be interrupted right before the second call runs. – R. Martinho Fernandes Oct 17 '12 at 07:45
  • yes that may happen as well, its given in the [JavaDoc](http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html#interrupted) – clinton Oct 17 '12 at 07:54
1

You can use Thread.interrupt() to tell a thread to stop. When that thread performs a some blocking operations or checks the flag it trigger it to throw an InterruptedException to either wake up the thread or stop it.

While the purpose of interrupt() is usually to stop a thread, it doesn't mean it will. The interrupt can be ignored for long periods of time or completely ignored. If it triggers an InterruptedException this can be caught and loggged (or ignored) rather than stopping the thread.

Note: For threads in an ExecutorService, interrupting a task interrupts the thread and the ExecutorService catches this by design and doesn't shutdown the thread.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    I'd probably not use "stop" in the this context. `Thread#interrupt` won't "stop" a thread that is not paying attention to the `interrupted` state – MadProgrammer Oct 17 '12 at 07:37
  • 1
    Also worth noting that a thread may not stop immediately, even if it does obey interruption requests. – Duncan Jones Oct 17 '12 at 07:38