CountdownLatch 结合了await(maxTime) 和countdown()
我有几个线程运行几乎无限的时间和迭代次数。当找到最佳解决方案时,迭代计数将重置为 0。设置最大迭代次数以防止无限循环。
当所有线程达到最大迭代次数时,我使用倒计时锁存器来停止该过程。换句话说,当一个线程达到最大迭代次数时,它会使用notifyThreadStop()通知我的主线程,当所有线程停止时,它会触发countdown()。
注意:我的线程在 FixThreadPool ExecutorService 内运行。
我想添加一个 maxTime 锁存器。所以我所做的是以下
List<Runnable> r = .... //(contains all my runnables)
myExecutorService.invokeAll(r);
if(maxtime > 0){
mylatch.await(maxTime,TimeUnit.Seconds);
(1)
do stuff...
exit;
}
else{
mylatch.await();
myExecutorService.shutdownNow();
do stuff...
exit;
}
现在,我知道如果倒计时触发了闩锁,则意味着所有线程都已停止,因此我可以 shutdownNow 我的 ExecutorService。
当达到最大时间时,情况并非如此。因此,在(1)中,我想遍历所有跑步者,以文明的方式终止它们:-)。为此,我定义了一个函数 requestTermination() ,简单地说,将我的可运行对象中的 iterationCounter 设置为 MaxIterationCount 。
所以(1)会变成
for(Runnable runner: r){
if(r.getIsRunning()){r.requestTermination();}
}
(2)
现在,我需要再次等待,直到所有线程真正停止,然后才能继续......嗯,只是想我可以有一个额外的锁存器并使用它。
所以 (2) 会变成
mylatch2.await();
myExecutorService.shutdownNow();
当然,我的函数 notifyThreadStop() 需要对其进行修改,并且需要一个标志告诉它在 mylatch2 而不是 mylatch 上执行 countdown() 。
我想我刚刚回答了我的问题,但既然所有这些都已经写下来,我将把它留在这里供其他人参考。
现在的问题是:有更好的方法来处理这个问题吗? (1) 或 (2) 中的 shutdownNow() 是唯一需要的吗? 知道我的线程必须在退出之前关闭自己的日志文件并关闭其内部可调用线程*ss*。
I have several threads running for an almost infinite time and number of iteration. The iteration count being reset to 0 when a best solution has been found. A max number of iteration is set to prevent an infinite loop.
I use a countdownlatch to stop the process when all thread have reach the max number of iteration. In other word, when a thread reach the max number of iteration, it notifies my main thread using notifyThreadStop() which, when all thread are stopped, triggers countdown().
Note: my threads are running inside a FixedThreadPool ExecutorService.
I would like to add a maxTime latch. So what i did is the following
List<Runnable> r = .... //(contains all my runnables)
myExecutorService.invokeAll(r);
if(maxtime > 0){
mylatch.await(maxTime,TimeUnit.Seconds);
(1)
do stuff...
exit;
}
else{
mylatch.await();
myExecutorService.shutdownNow();
do stuff...
exit;
}
Now, I know that if the countdown has triggered the latch, it means that all threads are stopped so I can shutdownNow my ExecutorService.
It is not the case when the max time has been reached. So in (1) i would like to iterate through all my runners to terminate them in a civilized way :-) . For that, i have defined a function requestTermination() that, simply put, set the iterationCounter to MaxIterationCount in my runnables.
So (1) would become
for(Runnable runner: r){
if(r.getIsRunning()){r.requestTermination();}
}
(2)
Now, I need to wait again until all threads are really stopped before i can proceed.... hmmmm just thinking that i could have an additional latch and work with that.
So (2) would become
mylatch2.await();
myExecutorService.shutdownNow();
Of course, my function notifyThreadStop() would need to be modified of it and would need a flag telling it to do a countdown() on mylatch2 as opposed to mylatch.
I think I have just answered my question but since all this has been written, I'll leave it here for others to refer to it.
The question would now be: Any better way of handling this? Would a shutdownNow() in (1) or (2) be the only thing required? Knowing that my my threads have to close their own log file and shutdown their inner Callable thread*ss* before exiting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 shutdownNow() 和awaitTernimation(),这将中断所有正在运行的任务并等待它们完成。
如果任务可以中断并在中断时正确关闭所有资源,那么应该不会有问题。 (如果有问题,这是您的任务代码中的错误,您应该修复)
If you use shutdownNow() and awaitTernimation() this will interrupt all running tasks and wait for them to finish.
Provided the tasks can be interuppted and close all resources correct when interrupt, there shouldn't be a problem. (If there is a problem, this is a bug in your task code which you should fix)
,你确实做到了:-) 如果需要添加一件事,那就是你应该小心 RuntimeExceptions,否则,你的 shutdownNow() 可能永远不会被调用。但你已经知道了,对吧?
You did, indeed :-) If there's one thing to add is that you should be careful with RuntimeExceptions, otherwise, your shutdownNow() may never be called. But you knew that already, right?