How to stop a thread in Java? Example

Today we're going to learn about how to stop a thread in Java. It's easy to start a thread in Java because you have a start() method but it's difficult to stop the thread because there is no working stop() method. Well, there was a stop() method in Thread class, when Java was first released but that was deprecated later. In today's Java version, You can stop a thread by using a boolean volatile variable.  If you remember, threads in Java start execution from run() method and stop, when it comes out of run() method, either normally or due to any exception. You can leverage this property to stop the thread. All you need to do is  create a boolean variable e.g. bExit or bStop. Your thread should check its value every iteration and comes out of the loop and subsequently from run() method if bExit is true. 

In order to stop the thread, you need to set the value of this boolean variable to true when you want to stop a running thread. Since you are setting this variable from a different thread e.g. main thread, it's important to mark this variable volatile, otherwise, it's possible for the running thread to cache its value and never check back to main memory for updated value and running infinitely.

When you make a variable volatile, running thread will not cache its value in its local stack and always refer main memory. The volatile variable also provides happens before guarantee, which can be used to synchronize values. If you want to understand multi-threading and concurrency in the right way, I strongly suggest reading Java Concurrency in Practice twice. It's one of the most accurate, thorough and practical books in Java multi-threading.

Stopping a Thread in Java - An Example

Here is our sample code example to stop a thread in Java. You can see that in this example, the main thread is first starting a thread and later it's stopping that thread by calling our stop() method, which uses a boolean volatile variable to stop running thread e.g. Server.  From the output, it's clear that our Server, which implements Runnable is running fine until main() method called the stop() method, the only then value of boolean volatile variable exit is set to true. In next iteration, our Server thread checks and find this value true, so it come out of the loop and run() method, which means your thread is now stopped.

Using boolean volatile variable to stop a thread in Java

Here is the Java program to demonstrate how to stop a thread in Java using boolean volatile variable, You can also see here for similar approach.

import static java.lang.Thread.currentThread;

import java.util.concurrent.TimeUnit;

 * Java Program to demonstrate how to stop a thread in Java.
 * There is a stop() method in Thread class but its deprecated 
 * because of deadlock and other issue, but its easy to write
 * your own stop() method to stop a thread in Java. 
 * @author java67

public class ThreadStopDemo {

    public static void main(String args[]) throws InterruptedException {
        Server myServer = new Server();

        Thread t1 = new Thread(myServer, "T1");
        //Now, let's stop our Server thread
        System.out.println(currentThread().getName() + " is stopping Server thread");
        //Let's wait to see server thread stopped 
        System.out.println(currentThread().getName() + " is finished now");

class Server implements Runnable{
    private volatile boolean exit = false;
    public void run() {
            System.out.println("Server is running.....");
        System.out.println("Server is stopped....");
    public void stop(){
        exit = true;

Server is running.....
Server is running.....
Server is running.....
Server is running.....
Server is running.....
Server is running.....
Server is running.....
Server is running.....
Server is running.....
Server is running.....
main is stopping Server thread
Server is running.....
Server is stopped....
main is finished now

Things to remember

1) Please ensure that boolean variable which is used to stop the thread is volatile, otherwise, in the worst case your thread may not stop and run infinitely, Why? because, in the absence of any synchronization instruction e.g. volatile modifier here, the compiler is free to cache the value of boolean variable exit, which means even if the main thread makes it true, Server will always see it false, thus running infinitely. This concept is often tested in Java interviews as well.

2) It's always better to check for a boolean variable in your while loop, it could be your game loop or main server loop used to process request.

3) It's good to provide a method to stop the server, which does nothing but change the value of boolean variable.

4) Using TimeUnit class to pause a thread is better than Thread.sleep() method because it improves readability. You know up front that whether thread is stopping for 2 millisecond or 2 second, which was not visible in case of Thread.sleep().  See here to learn more about TimeUnit class.

That's all about how to stop a thread in Java. Make sure to check for stopping condition in a while loop and ensure that the boolean volatile variable. This will guarantee two things, first the thread which is checking this boolean variable will not cache its value in its local cache and second any change made on one thread before setting this volatile boolean variable will be visible to other thread as well. In the real world, though, you may need to give your thread some time to finish pending task, release resources and do cleanup before stopping.

If you like this short tutorial and interested to learn Java in deep, here are couple of more interesting articles you will enjoy :
  • What is difference between transient and volatile variable in Java? [answer]
  • How to pause a thread in Java? [example]
  • What is difference between a thread and a process? [answer]
  • What is difference between implements Runnable and extends Thread in Java? [answer]
  • 10 things you should know about Thread in Java? [article]
  • When to use wait() and sleep() method in Java? [answer]
  • Difference between Callable and Runnable interface in Java? [answer]

Recommended book to master Java concurrency and multi-threading :
  • Java Concurrency in Practice by Brian Goetz  [see here]
  • Java Threads By Scott Oaks and Henry Wong [see here]


  1. You really should consider joining on the Server's thread, this is the only way to know for certain that the thread is finished.

    1. @Anonymous, you are correct, join() would have been better choice there, but I have opted sleep for simplicity.

    2. How is join() less simple than sleep()? I'd say it's the other way around.

  2. In the above example, If we use t1.join() after t1.start() , it will go in infinite loop, and the control would never return to main().

  3. @Unknown yes control will never go to main method .
    The join method allows one thread to wait for the completion of another.
    as in the code while(!exit){...} the value for exit is false it is never changing to true(as myServer.stop() called in main method ),so it will go to a infinite loop.