给定两个Java线程,当其中一个完成时停止一个线程 [英] Given two Java threads, stop one thread when one of them finishes

查看:247
本文介绍了给定两个Java线程,当其中一个完成时停止一个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为这个问题寻找一个干净的设计/解决方案:我有两个线程,可以运行只要用户想要,但最终停止时,用户发出停止命令。但是如果其中一个线程突然结束(例如由于运行时异常),我想停止另一个线程。

I'm looking for a clean design/solution for this problem: I have two threads, that may run as long as the user wants to, but eventually stop when the user issues the stop command. However if one of the threads ends abruptly (eg. because of a runtime exception) I want to stop the other thread.

现在两个线程都执行一个 Runnable (所以当我说'停止线程'的意思是我在 Runnable 实例调用stop()我想的是避免使用线程(Thread类)和使用 CompletionService 接口,然后提交这两个Runnables这个服务的实例。

Now both threads execute a Runnable (so when I say 'stop a thread' what I mean is that I call a stop() method on the Runnable instance), what I'm thinking is to avoid using threads (Thread class) and use the CompletionService interface and then submit both Runnables to an instance of this service.

这样,我将使用CompletionService的方法 take(),当这个方法返回时,我将停止两个Runnables,因为我知道至少一个已经完成。现在,这个工作,但如果可能的话,我想知道一个更简单/更好的解决方案为我的情况。

With this I would use the CompletionService's method take(), when this method returns I would stop both Runnables since I know that at least one of them already finished. Now, this works, but if possible I would like to know of a simpler/better solution for my case.

此外,什么是一个很好的解决方案,当我们< c $ c> n 线程,一旦其中一个完成停止执行所有其他线程?

Also, what is a good solution when we have n threads and as soon as one of them finishes to stop execution of all the others ?

提前感谢。

推荐答案

$ c> Runnable.stop()方法,因此这是一个明显的非初始方法。

There is no Runnable.stop() method, so that is an obvious non-starter.

不要使用 Thread.stop()

如果实施正确,这里有几种方法应该可以正常工作。

Here are a couple of approaches that should work, if implemented correctly.


  1. 你可以让两个线程定期检查一些常见的标志变量(例如调用 stopNow ),并安排这两个线程在完成后设置它。 (标志变量需要是volatile或者正确同步。)

  1. You could have both threads regularly check some common flag variable (e.g. call it stopNow), and arrange that both threads set it when they finish. (The flag variable needs to be volatile ... or properly synchronized.)

你可以让两个线程定期调用 Thread。 isInterrupted()方法来查看它是否已被中断。然后每个线程在完成后需要调用 Thread.interrupt()

You could have both threads regularly call the Thread.isInterrupted() method to see if it has been interrupted. Then each thread needs to call Thread.interrupt() on the other one when it finishes.








我知道Runnable没有该方法,但是我传递给线程的Runnable的实现它会在调用它时完成run()方法(类似Corsika的代码,下面的答案)。

I know Runnable doesn't have that method, but my implementation of Runnable that I pass to the threads does have it, and when calling it the runner will finish the run() method (something like Corsika's code, below this answer).

我可以告诉,Corsika的代码假设有一个 stop()方法,将调用时做正确的事情。真正的问题是你如何实现它?

From what I can tell, Corsika's code assumes that there is a stop() method that will do the right thing when called. The real question is how have you do implemented it? Or how do you intend to implement it?


  • 如果你已经有一个工作的实现,那么你有一个解决方案

  • If you already have an implementation that works, then you've got a solution to the problem.

否则,我的答案提供了实施立即停止功能的两种可能的方法。

Otherwise, my answer gives two possible approaches to implementing the "stop now" functionality.


我感谢您的建议,但我有疑问,定期检查/致电如何翻译成代码?

I appreciate your suggestions, but I have a doubt, how does 'regularly check/call' translate into code ?

这完全取决于 Runnable.run()方法执行的任务。它通常需要添加一个检查/调用到某些循环,使测试合理经常发生...但不是太频繁。你还想检查只有当它可以安全地停止计算,这是另一件你必须自己解决的事情。

It entirely depends on the task that the Runnable.run() method performs. It typically entails adding a check / call to certain loops so that the test happens reasonably often ... but not too often. You also want to check only when it would be safe to stop the computation, and that is another thing you must work out for yourself.

这篇关于给定两个Java线程,当其中一个完成时停止一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆