How to stop threads of a Java program?

I created a java program with a graphical interface and placed the "Stop" button on it. When I run the program, the main thread starts 10 threads. Now I want that whenever the user clicks the Stop button, all threads should end first, then the main stream should end. How can i do this.

+4
source share
6 answers

It depends on how you want the 10 threads to be “completed” and how you execute the threads.

I recommend creating an ExecutorService and writing your "threads" as Runnable . These Runnable objects must respond to interrupt() calls in their execution thread, interrupting their task, clearing and exiting run as quickly as possible.

Submit these Runnable tasks to the ExecutorService , then call awaitTermination , all in the main topic. When the user clicks the Stop button, call shutdown or shutdownNow as desired on the ExecutorService (from the event dispatch stream).

If you call shutdownNow in the executor’s service, it will notify the tasks in progress, interrupting their threads. If you call shutdown , this will allow you to complete tasks without interruption. No matter what, your main thread will block on awaitTermination until all tasks complete (or expire).

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

+12
source

First, let me note that there is a temptation method in the Thread class called stop() . Do not use it, it is dangerous .

One way to do this is to program your 10 threads to check for the interrupted thread status.

eg.

 while (! Thread.interrupted()) { // Do your thread 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 complete.

+6
source

Give all Threads a common instance of the logical element and let them periodically check if it is true . If it is true , Thread should return from the run method. Set this logic element to true if the user clicks the stop button, and then use Thread.join() to wait for all threads.

+5
source

Thread.stop () is deprecated, and it advises you to use the “running” flag, which is periodically checked by Thread, so it can end when you set the flag to false. If Thread has long waiting phases, you can abort () it to wake it from the wait () state. -> Thread.stop () int he Java API doc

After completing your threads, setting the execution condition to false, you can sequentially bind your Thread Thread () to other Threads to wait for them to complete.

+3
source

The suspend () and resume () methods of the Thread class are deprecated because they are essentially unsaved. See this article for information on why they are deprecated and methods for stopping threads:

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

+2
source

If your streams are waiting in an InputStream, just add some boolean flag to the streams and close the stream. Themes will wake up, check the flag, and exit. Same thing if they are waiting for the condition (Object.wait ()), use notifyAll () and set the flag. If your threads are constantly looping (which is BAD), just set the flag :)

0
source

All Articles