In this blog, we will guide you on interrupting threads in Java. If any thread is in a sleeping or a waiting state, we want to break out of that state. We use the interrupt() method in Java to interrupt a thread. The interrupt method breaks the state of waiting or sleeping thread and throws an Interrupt exception. Now, we will see types of Interrupt methods in Java that can be invoked on a thread.
Now, we will see some Java Programs to understand the interruption of threads better.
Examples
Example1
​​We will interrupt a thread that does not stop working in this example. When the program interrupts the currently executing thread, the thread will break out the sleeping state, but it will not stop working. When we apply the interrupt method on a thread, it will throw InterruptException We have handled the InterruptException using try-catch block.
// Java Program to see the concept of interrupt() method
// while a thread does not stops working
class Program extends Thread {
public void run()
{
try {
for (int i = 0; i < 10; i++) {
System.out.println("Executing Child thread");
// Here current threads go to sleeping state
// Another thread gets the chance to execute
Thread.sleep(2000);
}
}
catch (InterruptedException e) {
System.out.println("InterruptedException occurred");
}
}
}
class Main{
public static void main(String[] args)
throws InterruptedException
{
Program thread = new Program();
thread.start();
// main thread calls interrupt() method on child thread
thread.interrupt();
System.out.println("The Main thread execution completed");
}
}
Output
The Main thread execution completed
Executing Child thread
InterruptedException occurred
Example2
In the example, we will interrupt a thread that stops working. When the program interrupts the currently executing thread, it will throw a new exception in the catch block, so it will stop working.
// Java Program to illustrate the
// concept of interrupt() method
// while a thread stops working
class Program extends Thread {
public void run()
{
try {
Thread.sleep(2000);
System.out.println("Executing thread");
}
catch (InterruptedException e) {
throw new RuntimeException("Thread " +
"interrupted");
}
}
public static void main(String args[])
{
Program t1 = new Program();
t1.start();
try {
t1.interrupt();
}
catch (Exception e) {
System.out.println("Exception handled");
}
}
}
Output
Exception in thread "Thread-0" java.lang.RuntimeException: Thread interrupted
at Program.run(File.java:13)
Example3
In the example, we will interrupt a thread that is in a normal state. Here, we do not have any thread in waiting or sleeping state. So, there will never be an InterruptException. The interrupt() method will only set the interrupted flag to true to use this in the future.
// Java Program to show how to interrupt() a normal thread
class Program extends Thread {
public void run()
{
for (int i = 0; i < 5; i++)
System.out.println(i);
}
public static void main(String args[])
{
Program t1 = new Program();
t1.start();
t1.interrupt();
}
}
Output
0
1
2
3
4
Now, let's move to the FAQs section to clear your doubts regarding Interrupting threads.
What are interrupting threads? If any thread is in sleeping or waiting state, calling the interrupt() method on that thread breaks out the sleeping or waiting state throwing InterruptedException.
What happens if the thread is interrupted? When a thread is interrupted,an InterruptedException is thrown.It is a checked exception, and many blocking operations in Java can throw it.
How do I know if a thread is interrupted? The interrupted() is a static method in the Thread class that tells if the current thread has been interrupted. The isInterrupted() method checks if this thread instance has been interrupted.
Does interrupt stop a thread? The interrupt() does not stop the thread. The thread will continue to run.
What is the difference between an interrupt and a trap? The interrupt is a signal emitted by hardware for the CPU that indicates an event that requires immediate attention. In contrast, the trap is a signal raised by a user program instructing the operating system to perform some functionality immediately.
Key Takeaways
In this article, we extensively discussed Interrupting threads and different types of interrupt methods on Threads. And at the end of the blog, we have seen some examples to understand the working of the Interrupt methods.
We hope that this blog has helped you enhance your knowledge regarding Interrupting threads and if you would like to learn more, check out our articles in the code studio library. Do upvote our blog to help other ninjas grow. Happy Coding!