Home »
Java programming language
How to stop a thread in Java?
By Preeti Jain Last updated : January 26, 2024
There are no direct or shortcut ways to stop thread in Java. A thread stops when the execution of the run() method is completed normally or a thread stops if it raises an exception in the meanwhile of Thread completion.
Java does not provide some direct methods to stop threads in Java but there are some control methods defined in Java JDK earlier versions and the names of the method are given below:
- stop()
- suspend()
- resume()
All the methods given above are deprecated so it does not support in later versions of Java.
Stopping Thread Manually
There are two approaches to stop a thread:
- Using Volatile Boolean Variable
- Using interrupt() Method
Now we will elaborate on each way one by one is given below...
Using Volatile Boolean Variable
- In the first step, we will declare a volatile boolean variable in a thread.
- In the second step initially, we will assign the value of the volatile boolean variable as true.
- In the third step, we will define a job inside while loop and passing parameter of the volatile boolean variable in while loop and we will keep loop inside run() method. By using loop the thread will continue to run until volatile boolean variable becomes false.
- In the fourth step, we will define another method named stopThread() inside a thread and in this method, we will set volatile boolean variable is set to false to stop the thread.
- In the fifth or final step, we don't need to do anything manually to stop the thread. To stop a thread we can simply call user-defined stopThread() method to stop the thread.
Example
// Java program to stop a thread with the help of
// volatile boolean variable
class ChildThread extends Thread {
// intially assign boolean variable with value as true
volatile boolean thread_stop = true;
// this method is defined to stop a thread
public void stopThread() {
thread_stop = false;
}
// this loop will continue until boolean variable become false
public void run() {
while (thread_stop) {
System.out.println("Child Thread Started");
}
System.out.println("Child Thread Ended");
}
}
class Main {
public static void main(String[] args) {
ChildThread ct = new ChildThread();
ct.start();
try {
Thread.sleep(100);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
// this method will be called to stop a thread
ct.stopThread();
}
}
Output
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
.
.
.
.
.
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Ended
Using interrupt() Method
We will use readymade interrupt() method to stop a thread.
When we call interrupt method on a thread object and it will assign the interrupted status of a thread and this status can be generated by the interrupted() method.
Example
// Java program to stop a thread by using interrupt() method
class ChildThread extends Thread {
// this loop will continue until boolean variable become false
public void run() {
while (!Thread.interrupted()) {
System.out.println("Child Thread Started");
}
System.out.println("Child Thread Ended");
}
}
class Main {
public static void main(String[] args) {
ChildThread ct = new ChildThread();
ct.start();
try {
Thread.sleep(100);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
// this method will be called to interrupt a thread
ct.interrupt();
}
}
Output
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
.
.
.
.
.
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Started
Child Thread Ended