views:

994

answers:

6

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.

+3  A: 

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.

tangens
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
In the question there is only a stop button that sets this Boolean. No danger. It's only set once.
tangens
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
That said, by the way, calling interrupt on each thread probably makes more sense.
Bill K
+1  A: 

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:

http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html

leonm
+4  A: 

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.

e.g.

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.

Simon Nickerson
+8  A: 

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.

erickson
+2  A: 

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.

Peter Walser
A: 

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 :)

tulskiy