Interrupt

  • https://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html
  • https://www.youtube.com/watch?v=-7ZB-jpaPPo
  • An interrupt is a cooperative mechanism for a thread to signal another thread that it should, at its convenience and if it feels like it, stop what it is doing and do something else
  • A thread is said to be interrupted, if its interrupt status is set
  • It’s up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate
  • The only way (I know) to interrupt thread is by calling thread.interrupt() instance method from one thread to the another thread
  • For the interrupt mechanism to work correctly, the interrupted thread must support its own interruption

Interruptible methods

  • These methods generally poll for interruption before blocking or doing any work, so as to be as responsive to interruption as possible
  • They throw InterruptedException checked exception
  • Examples:
    • Thread.sleep()
    • BlockingQueue.put()
    • Thread.join()
  • When they throw exception, do they control interrupt flag of current thread?

Interrupt flag

  • aka interrupt status
  • Indication to a thread that it should stop what it is doing and do something else
  • It is common for the thread to terminate
  • By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so.
    • The idea is that an interrupt should be handled once
    • However, it’s always possible that interrupt status will immediately be set again, by another thread invoking interrupt
  • https://stackoverflow.com/questions/2523721/why-do-interruptedexceptions-clear-a-threads-interrupted-status
  • Following method throws Interrupted exception when current thread is interrupted and clears
  • thread.isInterrupted()
    • instance method
    • Tests if this thread has been interrupted
  • thread.interrupt()
    • instance method
    • set the interrupt flag, hence interrupting the thread
    • It does not stop the execution of the flow
    • Used for example, if a thread is sleeping then interrupting the thread will cause the sleep to be interrupted and throw InterruptedException
      • Similar for join
      • Only one sleep can be interrupted by one interrupt
    • Can be called as Thread.currentThread().interrupt()
  • Thread.interrupted()
    • static method
    • Tests if the current thread has been interrupted AND clear the interrupted flag
    • Handling of interrupt is done generally by polling via this method which returns the current thread’s interrupted status AND clears that interrupt flag.
      • Usually the thread might then do something such as throw InterruptedException
      • Since interrupt status is cleared automatically, then it is not need to be done
  • As soon as the thread is interrupted, we want to clear the interrupt status flag and decide what to do with the interrupt. This helps to check other interrupt status in the future
  • https://stackoverflow.com/questions/3590000/what-does-java-lang-thread-interrupt-do

Setting and clearing Interrupt flag

  • Setting the flag can ONLY be done using thread.interrupt()
  • Clearing the flag can ONLY be done explicitly using Thread.interrupted() testing method which is used to poll for thread interruption
  • Implicitly some methods which throw InterruptedException clears the interrupt status as well for the current thread. For example:
    • Thread.sleep
    • Object.wait
    • Thread.join
try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    // interrupt flag is already cleared here
    // because Thread.sleep caught an interrupt for the current thread
    // and threw exception after clearning the interrupt internally
}

InterruptedException

if (Thread.interrupted())  // Clears interrupted status!
      throw new InterruptedException();

Exceptions

thread.setUncaughtExceptionHandler(h);
thread.start();
  • Following throws InterruptedException on the current thread:
    • Thread.sleep() is natively implemented
    • thread.join() internally is implemented by wait() which is natively implemented

Not responsive to interruption

  • These do not behave properly to interrupts
    • java.io: Synchronous I/O: read and write
    • java.nio: Synchronous I/O
    • java.nio: Asynchronous I/O with selector

JVM shutdown

  • Types:
    • orderly shutdown
    • abruptly shutdown
  • Orderly Shutdown
    • calling System.exit()
    • sending SIGINT (for example by pressing Ctrl + C)
    • it calls all the registered shutdown hooks concurrently
    • order of execution of shutdown hooks is not guaranteed
    • when all the shutdown hooks are completed, finalizers are run if configured
    • if shutdown hooks or finalizers don’t complete, then JVM hangs and you need to shutdown abruptly
  • Abrupt Shutdown
    • calling Runtime.getRuntime().halt()
    • sending SIGKILL (for example by running kill -9)
    • JVM will simply halt, and not run shutdown hooks