A thread is automatically destroyed when the run() method has completed. But it might be required to kill/stop a thread before it has completed its life cycle. Previously, methods suspend(), resume() and stop() were used to manage the execution of threads. But these methods were deprecated by Java 2 because they could result in system failures. Modern ways to suspend/stop a thread are by using a boolean flag and Thread.interrupt() method.
- Using a boolean flag: We can define a boolean variable which is used for stopping/killing threads say ‘exit’. Whenever we want to stop a thread, the ‘exit’ variable will be set to true.
// Java program to illustrate// stopping a thread using boolean flagclassMyThreadimplementsRunnable {// to stop the threadprivatebooleanexit;privateString name;Thread t;MyThread(String threadname){name = threadname;t =newThread(this, name);System.out.println("New thread: "+ t);exit =false;t.start();// Starting the thread}// execution of thread starts from run() methodpublicvoidrun(){inti =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 threadpublicvoidstop(){exit =true;}}// Main classpublicclassMain {publicstaticvoidmain(String args[]){// creating two objects t1 & t2 of MyThreadMyThread t1 =newMyThread("First thread");MyThread t2 =newMyThread("Second thread");try{Thread.sleep(500);t1.stop();// stopping thread t1t2.stop();// stopping thread t2Thread.sleep(500);}catch(InterruptedException e) {System.out.println("Caught:"+ e);}System.out.println("Exiting the main Thread");}}chevron_rightfilter_noneOutput: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: 5 Second thread Stopped. First thread Stopped. Exiting the main Thread
Note: The output may vary every time.
By using a flag we can stop a thread whenever we want to and we can prevent unwanted run-time errors.
Using a volatile boolean flag: We can also use a volatile boolean flag to make our code thread safe. A volatile variable is directly stored in the main memory so that threads cannot have locally cached values of it. A situation may arise when more than one threads are accessing the same variable and the changes made by one might not be visible to other threads. In such a situation, we can use a volatile boolean flag.
Let’s consider the code below where we use a non-volatile boolean flag:
// Java program to illustrate non-volatile boolean flagpublicclassMain {// static used here// because a non-static variable// cannot be referenced// from a static context// exit variable to stop both// the main and inside threadsstaticbooleanexit =false;publicstaticvoidmain(String[] args){System.out.println("started main thread..");// a thread inside main threadnewThread() {publicvoidrun(){System.out.println("started inside thread..");// inside thread caches the value of exit,// so changes made to exit are not visible herewhile(!exit)// will run infinitely{}// this will not be printed.System.out.println("exiting inside thread..");}}.start();try{Thread.sleep(500);}catch(InterruptedException e) {System.out.println("Caught :"+ e);}// so that we can stop the threadsexit =true;System.out.println("exiting main thread..");}}chevron_rightfilter_noneRuntime Errors:
Time Limit Exceeded
Output:
started main thread.. started inside thread.. exiting main thread..
Note: The above code runs into an infinite loop and will give run-time error.
The output above shows that the inside thread is never stopped. This happens because the change made to ‘exit’ variable in the main thread is not visible to the inside thread. This is so because the inside thread locally caches the value of exit. To prevent this from happening we can use a volatile variable. The code below illustrates it.
// Java program to illustrate volatile boolean flagpublicclassMain {// static used here because// a non-static variable cannot be referenced// from a static context// exit variable to stop both// the main and inside threadsstaticvolatilebooleanexit =false;publicstaticvoidmain(String[] args){System.out.println("started main thread..");// a thread inside main threadnewThread() {publicvoidrun(){// changes made to exit// in main thread are visible hereSystem.out.println("started inside thread..");// will not run infinitelywhile(!exit) {}// this will be printedSystem.out.println("exiting inside thread..");}}.start();try{Thread.sleep(500);}catch(InterruptedException e) {System.out.println("Caught :"+ e);}// so that we can stop the threadsexit =true;System.out.println("exiting main thread..");}}chevron_rightfilter_noneOutput:started main thread.. started inside thread.. exiting main thread.. exiting inside thread..
The output above shows that when we use a volatile boolean flag, we do not run into infinite loops. This is because the volatile variable directly stored in the main memory. In other words, changes made by one thread are visible to other threads. Thus using volatile makes our code, thread safe.
- Using Thread.interrupt() method: Whenever an interrupt has been sent to a thread, it should stop whatever task it is performing. It is very likely that whenever the thread receives an interrupt, it is to be terminated. This action can be done by using the interrupt() method. Whenever Thread.interrput() is called, it sets a flag known as the interrupt status to true. This means that the thread has to stop performing further execution. The default value of this flag is false.
// Java program to illustrate// stopping a thread// using the interrupt() methodclassMyThreadimplementsRunnable {Thread t;MyThread(){t =newThread(this);System.out.println("New thread: "+ t);t.start();// Starting the thread}// execution of thread starts from run() methodpublicvoidrun(){while(!Thread.interrupted()) {System.out.println("Thread is running");}System.out.println("Thread has stopped.");}}// Main classpublicclassMain {publicstaticvoidmain(String args[]){// creating objects t1 of MyThreadMyThread t1 =newMyThread();try{Thread.sleep(1);// t1 is an object of MyThread// which has an object t// which is of type Threadt1.t.interrupt();Thread.sleep(5);}catch(InterruptedException e) {System.out.println("Caught:"+ e);}System.out.println("Exiting the main Thread");}}chevron_rightfilter_noneOutput: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 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
Note: The output may vary every time.
Recommended Posts:
- Difference Between Daemon Threads and User Threads In Java
- Joining Threads in Java
- Output of Java program | Set 16 (Threads)
- Introducing Threads in Socket Programming in Java
- Producer-Consumer solution using threads in Java
- Green vs Native Threads and Deprecated Methods in Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




