interrupt(), interrupted() and isInterrupted() in Java Multithreading
In this post, we will understand about interrupt()
, interrupted()
and isInterrupted()
methods in Java Multithreading.
If you are interested in reading more about interruption mechanism you can look into our post InterruptedException in Java Multithreading.
In Java, thread starts its execution when we call start()
method on Thread object. It internally calls overridden run()
method. 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 server with 5 different threads, 10 files using each thread. Now lets say you want to provide a button to cancel the download. How you will do that? You need to shutdown 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 hitting Ctrl + C
.
Lets put this infinite loop into 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 shutdown a thread cleanly?
In Java, starting a threads is easy, but shutting them down require 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.
-
interrupt() : When we call
loop.interrupt()
, an Interrupt status flag is set totrue
. -
interrupted() : It is
static
method and checks the interrupt status flag of current thread. When we callinterrupted()
, the flag is returned and immediately set tofalse
.
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 🙂
If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.
We may not get time to plant a tree, but we can definitely donate ₹42 per Tree.
About the Author
Tags: Concurrency, Core Java, Java, Multithreading, Thread, x vs y
Comments and Queries
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:<pre><code class="java"> String foo = "bar"; </code></pre>For more information on supported HTML tags in disqus comment, click here.