How to stop threads of a Java program?

I have made a java program with GUI and have placed a "stop" button on it. When I start the program, the main thread starts 10 threads. Now I want that whenever a user click on "stop" button, all the threads should terminate first, then the main thread should terminate. How can I do that.

13.10.2009 19:35:37
I think you need to supply some code with your question, as this could have something to do with your initialization of the threads.
Steen 13.10.2009 19:38:00

Give all Threads a shared instance of a Boolean and let them check periodically if it is true. If it's true the Thread should return from its run method. Set this Boolean to true if the user presses the stop button and then use Thread.join() to wait for all Threads.

13.10.2009 19:40:59
And what would happen if the flipping would be done in the traditional b=!b; way and two separate threads would try to flip the controller boolean? :) Certainly can be worked around but not the solution I'd prefer.
Esko 13.10.2009 19:49:11
In the question there is only a stop button that sets this Boolean. No danger. It's only set once.
tangens 13.10.2009 19:51:15
Why on earth would you flip the boolean with b=!b? It would be terminate=true or running=false. any =! would just be opening you up for a bug.
Bill K 13.10.2009 19:59:49
That said, by the way, calling interrupt on each thread probably makes more sense.
Bill K 13.10.2009 20:00:21
Is this interrupt not just a boolean in disguise? It is still no real interrupt since it inside the thread you need to check if the thread is interrupted.
Silver 14.12.2013 13:23:01

The suspend() and resume() methods on the Thread class are deprecated because they are inherently unsave. Look at this article for information on why they were deprecated and techniques to stop threads:

13.10.2009 19:43:00

Firstly, let me note that there is a tempting method on the Thread class called stop(). Do not use it, it is dangerous.

One way of doing this is to code your 10 threads to check the interrupted status of the thread.


while (! Thread.interrupted())
     // Do your thread's work

You can interrupt each worker thread by calling the interrupt() method on the Thread object, and then calling join() to wait for the thread to actually finish.

13.10.2009 19:43:16

It depends on how you want to the 10 threads to "terminate", and how you are running the threads.

I recommend creating an ExecutorService, and writing your "threads" as Runnable implementations. These Runnable objects should respond to calls to interrupt() on their thread of execution by aborting their task, cleaning up, and exiting the run method as quickly as possible.

Submit these Runnable tasks to an ExecutorService then call awaitTermination, all in the main thread. When the user presses the "stop" button, call shutdown or shutdownNow as desired on the ExecutorService (from the event dispatch thread).

If you call shutdownNow on the executor service, it will notify the running tasks by interrupting their threads. If you call shutdown, it will allow the tasks to complete without interruption. Regardless, your main thread will block on awaitTermination until all of the tasks have completed (or the time limit runs out).

You can, of course, create and manage of all of the threads yourself, using join. The key is to make the threads interruptible if you want to be able to stop them prematurely.

13.10.2009 19:50:43

Thread.stop() is deprecated, and it's adviced that you use a 'running' flag that is periodically checked by the Thread so it can terminate itself when you set the the flag to false. If the Thread has long wait phases, you can interrupt() it to wake it up from a wait() state. -> Thread.stop() int he Java API doc

After terminating your Threads by setting the running condition to false, you can have your main Thread join() the other Threads sequentially to wait for their termination.

13.10.2009 19:46:41

If your threads are waiting on an InputStream then just add some boolean flag to threads and close the stream. Threads will wakeup, check the flag and exit. Same if they are waiting on a Condition (Object.wait()), use notifyAll() and set the flag. If your threads are constantly looping (which is BAD), just set the flag :)

13.10.2009 20:42:58