Provide Best Programming Tutorials

Interrupt(), Interrupted() And IsInterrupted() In Java Multithreading

In this post, we will understand about,interrupt()interrupted() and  isInterrupted() methods in Java Multithreading.

In Java, the thread starts its execution when we call method start() on the Thread object. It internally calls the overridden methodrun(). The thread is said to be terminated when execution of run() method completes.

Shutting down thread forcefully means stopping the execution of thread, even if it has not yet completed execution of run()method.

In real-time applications, you may come across a scenario where you need to use multiple threads to complete the task and there may be a requirement to forcefully shutdown all your threads on triggering of some event.

For example, You are downloading 50 files from the server with 5 different threads, 10 files using each thread. Now let’s say you want to provide a button to cancel the download.  How you will do that? You need to shut down all five threads.

Let’s start with this code:

while (true) {
  // Do Nothing
}

It keeps on executing infinitely. It will only stop until JVM will stop or we will kill the process by hittingCtrl + C

Lets put this infinite loop into the thread:

Thread loop = new Thread(
  new Runnable() {
    @Override
    public void run() {
      while (true) {
         // Do Nothing
      }
    }
  }
);
loop.start();
// Now how do we stop it?

How Do We Stop A Thread When We Need It To Be Stopped?

If we are using JDK 1.0, we can call Thread’s deprecated method stop() to terminate it. Using stop() is incredibly dangerous, as it will kill your thread even if it is in the middle of something important. There is no way to protect yourself, so if you spot code that uses stop()you should frown.

How do we shut down a thread cleanly?

In Java, starting a thread is easy, but shutting them down requires a lot of attention and efforts.

Here is how it is designed in Java. There is a flag called Interrupt status flag in every java thread that we can set from the outside i.e. parent or main thread. And the thread may check it occasionally and stops its execution. Voluntarily..!! Here is how:

Thread loop = new Thread(
  new Runnable() {
    @Override
    public void run() {
      while (true) {
        if (Thread.interrupted()) {
          break;
        }
        // Continue to do nothing
      }
    }
  }
);
loop.start();
loop.interrupt();

There are two methods that are used in this example.

  1. interrupt(): When we call loop.interrupt(), an Interrupt status flag is set to true
  2. interrupted(): It is static method and checks the interrupt status flag of the current thread. When we call interrupted(), the flag is returned and immediately set to false.

Thus, if we never call Thread.interrupted() inside the run method and don’t exit when the flag is true, nobody will be able to stop us.

In other words, parent thread will ask child thread to stop by calling interrupt()method,  but child thread will just ignore these calls.

As per the tutorial on concurrency in Java Documentation

An interrupt is an indication to a thread that it should stop what it is doing and do something else. 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

isInterrupted() : There is one more method about which you should know is isInterrupted(). It is an instance method which returns the value of Interrupt status flag of a Thread object for which it is called on.

Interrupted()  Vs IsInterrupted()

interrupted() isInterrupted()
1 The interrupted() is a static method in Thread class that determines if the current thread has been interrupted. The isInterrupted() is an instance method that tests if this thread instance has been interrupted.
1  “The interrupted status of the thread is cleared by this method“. Therefore, if a thread was interrupted, calling interrupted() once would return true, while a second call to it would return false until the current thread is interrupted again.  “The interrupted status of the thread is unaffected by this method“.

I strongly recommend you to also read about InterruptedException in Java Multithreading to know more about interruption mechanism.

That’s all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.

Happy Learning ?

Leave a Reply

Close Menu