Interrupting a thread means stopping the execution of a thread prematurely. It can be done in Java using the interrupt()
method of the Thread
class. When interrupt()
is called on a thread, it sets the thread’s interrupted status to true. If the thread is blocked or sleeping, it will be interrupted immediately and throw an InterruptedException
. However, if the thread is running, it is up to the thread itself to handle the interruption.
To interrupt a thread, you can call the interrupt()
method on the thread object. For example, if t
is a thread object, you can interrupt it using t.interrupt()
.
Here is an example of interrupting a thread:
Thread myThread = new Thread(() -> { try { while (!Thread.currentThread().isInterrupted()) { // do some work } } catch (InterruptedException e) { // Handle the interruption } }); // Start the thread myThread.start(); // Interrupt the thread after 5 seconds Thread.sleep(5000); myThread.interrupt();
In this example, the myThread
thread runs a loop until it is interrupted. The loop checks the interrupted()
status of the thread and exits if it is set to true. The sleep()
method is used to simulate some work being done by the thread. After 5 seconds, the interrupt()
method is called to interrupt the thread. If the thread is blocked in the sleep()
method, it will immediately throw an InterruptedException
. Otherwise, the thread will continue running until it checks the interrupted()
status and exits the loop.
Example of interrupting a thread that stops working:
Sure, here is an example of interrupting a thread that stops working:
class MyThread extends Thread { public void run() { try { while (!Thread.currentThread().isInterrupted()) { // do some work here } } catch (InterruptedException e) { System.out.println("Thread interrupted"); return; } System.out.println("Thread stopped working"); } } public class Main { public static void main(String[] args) throws InterruptedException { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(5000); myThread.interrupt(); myThread.join(); System.out.println("Main thread finished"); } }
In this example, the MyThread
class extends Thread
and overrides the run
method to do some work in a loop until it is interrupted. If the thread is interrupted while doing the work, it catches the InterruptedException
and prints a message before returning from the method. If the thread is not interrupted, it prints a message when it stops working.
In the Main
class, a new instance of MyThread
is created and started. After waiting for 5 seconds, the interrupt
method is called to interrupt the thread. The join
method is called to wait for the thread to finish, and then a message is printed to indicate that the main thread has finished.
When you run this program, it should output something like this:
Thread interrupted Main thread finished
This indicates that the thread was interrupted and stopped working before the main thread finished.
Example of interrupting a thread that doesn’t stop working:
Sure, here is an example of interrupting a thread that doesn’t stop working:
class MyThread extends Thread { public void run() { while (true) { try { // do some work here Thread.sleep(1000); } catch (InterruptedException e) { System.out.println("Thread interrupted"); return; } } } } public class Main { public static void main(String[] args) throws InterruptedException { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(5000); myThread.interrupt(); System.out.println("Main thread finished"); } }
In this example, the MyThread
class extends Thread
and overrides the run
method to do some work in an infinite loop. The work is done using a sleep
method to simulate some processing. If the thread is interrupted while sleeping, it catches the InterruptedException
and prints a message before returning from the method.
In the Main
class, a new instance of MyThread
is created and started. After waiting for 5 seconds, the interrupt
method is called to interrupt the thread. Then, a message is printed to indicate that the main thread has finished.
When you run this program, it should output something like this:
Thread interrupted Main thread finished
This indicates that the thread was interrupted, but it did not immediately stop working because it was blocked in the sleep
method. When the thread wakes up from the sleep
, it checks its interrupted status and returns from the method.
Example of interrupting thread that behaves normally:
Sure, here is an example of interrupting a thread that behaves normally:
class MyThread extends Thread { public void run() { System.out.println("Thread started"); for (int i = 0; i < 10; i++) { // do some work here System.out.println("Thread working"); } System.out.println("Thread finished"); } } public class Main { public static void main(String[] args) throws InterruptedException { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(5000); myThread.interrupt(); System.out.println("Main thread finished"); } }
In this example, the MyThread
class extends Thread
and overrides the run
method to do some work in a loop. The work is done using a simple for
loop that iterates 10 times and prints a message to indicate that the thread is working. When the loop finishes, the thread prints a message to indicate that it has finished.
In the Main
class, a new instance of MyThread
is created and started. After waiting for 5 seconds, the interrupt
method is called to interrupt the thread. Then, a message is printed to indicate that the main thread has finished.
When you run this program, it should output something like this:
Thread started Thread working Thread working Thread working Thread working Thread working Thread finished Main thread finished
This indicates that the thread behaved normally and finished its work before the main thread finished. The interrupt had no effect because the thread was not blocked or sleeping.
What about isInterrupted and interrupted method?
isInterrupted
and interrupted
are two methods in the Thread
class that can be used to check the interrupted status of a thread. Here’s a brief explanation of how they work:
isInterrupted
: This is an instance method that returns a boolean value indicating whether the thread has been interrupted. It does not clear the interrupted status of the thread.interrupted
: This is a static method that returns a boolean value indicating whether the current thread has been interrupted. It also clears the interrupted status of the thread.
It’s important to note that calling interrupt()
on a thread only sets the interrupted status of the thread to true. It does not actually interrupt or stop the thread. It’s up to the code running in the thread to periodically check the interrupted status using isInterrupted
and decide whether to stop or continue working.
Here’s an example of how isInterrupted
and interrupted
can be used:
class MyThread extends Thread { public void run() { while (!isInterrupted()) { // do some work here } System.out.println("Thread interrupted: " + isInterrupted()); System.out.println("Thread finished"); } } public class Main { public static void main(String[] args) throws InterruptedException { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(5000); myThread.interrupt(); System.out.println("Main thread finished"); System.out.println("Current thread interrupted: " + Thread.interrupted()); } }
In this example, the MyThread
class extends Thread
and overrides the run
method to do some work in a loop until the thread is interrupted. When the thread is interrupted, it prints a message to indicate that it has been interrupted and whether the interrupted status is still set.
In the Main
class, a new instance of MyThread
is created and started. After waiting for 5 seconds, the interrupt
method is called to interrupt the thread. Then, a message is printed to indicate that the main thread has finished. Finally, the interrupted
method is called to check the interrupted status of the current thread and clear the status.
When you run this program, it should output something like this:
Thread interrupted: true Thread finished Main thread finished Current thread interrupted: false
This indicates that the thread was interrupted and stopped working, the main thread finished, and the interrupted status of the current thread was cleared by the interrupted
method.