How to stop a running thread in Java
In this tutorial, we will learn what is thread and how can we stop a running thread in Java. Threads are part of multithreading in Java that are used to execute multiple blocks of codes independently in java. Every process has at least one thread, which is the ‘Main Thread’. It is created by the Java Virtual Machine(JVM) at the program’s start, when the main() method is invoked.
Java program to stop a running thread
A java thread is a series of executed statements that help to allow doing multiple activities in a single process. Each thread is an independent and lightweight process.
Here we will see ways to kill a running thread. Earlier in java, there were three methods that managed the execution of thread i.e. suspend(), resume(), stop(). Though these methods were denounced later because these can lead to system failures. So the ways by which we can kill a thread are boolean flag method and Thread.interrupt() function.
Let’s see each of them one by one:
1. USING BOOLEAN FLAG METHOD:-
In this method, we declare and define a boolean variable whose value decides the execution of the thread. If its value is false, the execution will take place, if its value is true, the thread will truncate. We implement thread using Runnable class and run() function. Initially, the exit boolean variable will be set to false, and in stop() function, its value will be changed to true, hence the thread will stop.
Example:-
lass MyThread implements Runnable { private boolean exit; //boolean to stop thread. private String name; Thread thread; MyThread(String threadname) { name = threadname; thread = new Thread(this, name); System.out.println("New thread: " + thread); exit = false; // boolean set to false. thread.start(); // Starting the thread } // run function to start thread execution public void run() { int i = 0; while (!exit) { System.out.println(name + ": " + i); i++; try { Thread.sleep(100); } catch (InterruptedException e) { System.out.println("Caught:" + e); } } System.out.println(name + " Stopped."); } // for stopping the thread public void stop() { exit = true; } } public class stopThread { public static void main(String args[]) { // creating two objects t1 & t2 of MyThread MyThread thread1 = new MyThread("First thread"); MyThread thread2 = new MyThread("Second thread"); try { Thread.sleep(500); thread1.stop(); // stopping thread t1 thread2.stop(); // stopping thread t2 Thread.sleep(500); } catch (Exception e) { System.out.println("Caught:" + e); } System.out.println("Exiting the main Thread"); } }
Output:
New thread: Thread[First thread,5,main] New thread: Thread[Second thread,5,main] First thread: 0 Second thread: 0 First thread: 1 Second thread: 1 First thread: 2 Second thread: 2 First thread: 3 Second thread: 3 First thread: 4 Second thread: 4 First thread Stopped. Second thread Stopped. Exiting the main Thread
2. USING Thread.interrupt() METHOD:
When main calls interrupt method, it sets interrupt status to true and the thread stops executing whatever task it is performing. Following example depicts the execution of interrupt method:
class MyThread implements Runnable { Thread thread; MyThread() // constructor { thread = new Thread(this); System.out.println("New thread: " + thread); thread.start(); // Starting the thread } // run function to execute thread public void run() { while (!Thread.interrupted()) { System.out.println("Thread is running"); } System.out.println("Thread has stopped."); } } // Main class public class killThread{ public static void main(String args[]) { // instance of mythread MyThread thread1 = new MyThread(); try { Thread.sleep(1); thread1.thread.interrupt(); //interrupt is function of thread class which is a part of thread1 object. Thread.sleep(10); } catch (InterruptedException e) { System.out.println("Caught:" + e); } System.out.println("Exiting the main Thread"); } }
Output:
New thread: Thread[Thread-0,5,main] Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread is running Thread has stopped. Exiting the main Thread
Hence, in these two ways, we can kill or stop an executing thread in Java. I hope this tutorial was helpful to understand the concept.
Thank You.
For more examples, visit: Multithreading in Java
Woah, i was having a doubt for the same and couldn’t find any solution anywhere saw this article and it helped .
Thanks <3
Thanks for the implementation, it’s one of the best explanation I’ve seen. It helped me alot to understand how threads work.