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.
我用GUI创建了一个java程序,并在其上放置了一个“停止”按钮。当我启动程序时,主线程启动10个线程。现在我希望每当用户点击“停止”按钮时,所有线程应首先终止,然后主线程应该终止。我怎样才能做到这一点。
6 个解决方案
#1
12
It depends on how you want to the 10 threads to "terminate", and how you are running the threads.
这取决于你想要10个线程“终止”的方式,以及你如何运行线程。
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.
我建议创建一个ExecutorService,并将“线程”编写为Runnable实现。这些Runnable对象应该通过中止其任务,清理并尽快退出run方法来响应对其执行线程的interrupt()的调用。
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).
将这些Runnable任务提交给ExecutorService,然后在主线程中调用awaitTermination。当用户按下“停止”按钮时,在ExecutorService(来自事件调度线程)上根据需要调用shutdown或shutdownNow。
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).
如果在执行程序服务上调用shutdownNow,它将通过中断其线程来通知正在运行的任务。如果您调用shutdown,它将允许任务完成而不会中断。无论如何,您的主线程将阻止awaitTermination,直到所有任务都完成(或时间限制用完)。
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.
当然,您可以使用join自己创建和管理所有线程。关键是如果你想要过早地阻止线程,可以使线程中断。
#2
6
Firstly, let me note that there is a tempting method on the Thread
class called stop()
. Do not use it, it is dangerous.
首先,我要注意Thread类上有一个名为stop()的诱人方法。不要使用它,这是危险的。
One way of doing this is to code your 10 threads to check the interrupted status of the thread.
一种方法是编写10个线程来检查线程的中断状态。
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.
您可以通过调用Thread对象上的interrupt()方法来中断每个工作线程,然后调用join()以等待线程实际完成。
#3
5
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.
给所有线程一个布尔的共享实例,让它们定期检查它是否为真。如果确实是Thread应该从其run方法返回。如果用户按下停止按钮然后使用Thread.join()等待所有线程,则将此布尔值设置为true。
#4
3
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
不推荐使用Thread.stop(),并且建议您使用由Thread定期检查的“running”标志,以便在将该标志设置为false时自行终止。如果线程有很长的等待阶段,你可以中断()它从wait()状态唤醒它。 - > Thread.stop()int他是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.
通过将运行条件设置为false来终止线程后,您可以按顺序让主线程join()其他线程等待它们的终止。
#5
2
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:
不推荐使用Thread类上的suspend()和resume()方法,因为它们本身就是未保存的。查看本文以获取有关它们被弃用的原因以及停止线程的技术的信息:
http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html
http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html
#6
0
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 :)
如果你的线程在InputStream上等待,那么只需在线程中添加一些布尔标志并关闭流。线程将被唤醒,检查标志并退出。如果他们在条件(Object.wait())上等待,则使用notifyAll()并设置标志。如果你的线程不断循环(这是不好的),只需设置标志:)
#1
12
It depends on how you want to the 10 threads to "terminate", and how you are running the threads.
这取决于你想要10个线程“终止”的方式,以及你如何运行线程。
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.
我建议创建一个ExecutorService,并将“线程”编写为Runnable实现。这些Runnable对象应该通过中止其任务,清理并尽快退出run方法来响应对其执行线程的interrupt()的调用。
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).
将这些Runnable任务提交给ExecutorService,然后在主线程中调用awaitTermination。当用户按下“停止”按钮时,在ExecutorService(来自事件调度线程)上根据需要调用shutdown或shutdownNow。
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).
如果在执行程序服务上调用shutdownNow,它将通过中断其线程来通知正在运行的任务。如果您调用shutdown,它将允许任务完成而不会中断。无论如何,您的主线程将阻止awaitTermination,直到所有任务都完成(或时间限制用完)。
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.
当然,您可以使用join自己创建和管理所有线程。关键是如果你想要过早地阻止线程,可以使线程中断。
#2
6
Firstly, let me note that there is a tempting method on the Thread
class called stop()
. Do not use it, it is dangerous.
首先,我要注意Thread类上有一个名为stop()的诱人方法。不要使用它,这是危险的。
One way of doing this is to code your 10 threads to check the interrupted status of the thread.
一种方法是编写10个线程来检查线程的中断状态。
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.
您可以通过调用Thread对象上的interrupt()方法来中断每个工作线程,然后调用join()以等待线程实际完成。
#3
5
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.
给所有线程一个布尔的共享实例,让它们定期检查它是否为真。如果确实是Thread应该从其run方法返回。如果用户按下停止按钮然后使用Thread.join()等待所有线程,则将此布尔值设置为true。
#4
3
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
不推荐使用Thread.stop(),并且建议您使用由Thread定期检查的“running”标志,以便在将该标志设置为false时自行终止。如果线程有很长的等待阶段,你可以中断()它从wait()状态唤醒它。 - > Thread.stop()int他是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.
通过将运行条件设置为false来终止线程后,您可以按顺序让主线程join()其他线程等待它们的终止。
#5
2
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:
不推荐使用Thread类上的suspend()和resume()方法,因为它们本身就是未保存的。查看本文以获取有关它们被弃用的原因以及停止线程的技术的信息:
http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html
http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html
#6
0
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 :)
如果你的线程在InputStream上等待,那么只需在线程中添加一些布尔标志并关闭流。线程将被唤醒,检查标志并退出。如果他们在条件(Object.wait())上等待,则使用notifyAll()并设置标志。如果你的线程不断循环(这是不好的),只需设置标志:)