How to stop a thread in Java? Example (original) (raw)

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 the run() method and stop, when it comes out of the 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 like. bExit or bStop. Your thread should check its value every iteration and comes out of the loop and subsequently from the 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, the running thread will not cache its value in its local stack and always refer to the 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 the main() method called the stop() method, the only then value of boolean volatile variable exit is set to true.

In the next iteration, our Server thread checks and find this value true, so it comes out of the loop and runs () method, which means your thread is now stopped. See these Java Concurrency courses to learn more about it.

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 a boolean volatile variable, You can also see here for a similar approach.

import static java.lang.Thread.currentThread;

import java.util.concurrent.TimeUnit;

/**

public class ThreadStopDemo {

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

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

}

class Server implements Runnable{ private volatile boolean exit = false;

public void run() {
    while(!exit){
        System.out.println("Server is running.....");
    }
    
    System.out.println("Server is stopped....");
}

public void stop(){
    exit = true;
}

}

Output 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 the 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 like 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, the 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 requests.

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

4. Using TimeUnit class to pause a thread is better than the Thread.sleep() method because it improves readability. You know upfront that whether a thread is stopping for 2 milliseconds or 2 seconds, which was not visible in the 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 conditions 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.

If you like this short tutorial and are interested to learn Java in deep, here are a couple of more interesting articles you will enjoy :

In the real world, though, you may need to give your thread some time to finish the pending tasks, release resources and do cleanup before stopping.