CountdownLatch 结合了await(maxTime) 和countdown()

发布于 2024-10-16 01:23:43 字数 1412 浏览 3 评论 0原文

我有几个线程运行几乎无限的时间和迭代次数。当找到最佳解决方案时,迭代计数将重置为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彻夜缠绵 2024-10-23 01:23:43

如果您使用 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)

伤痕我心 2024-10-23 01:23:43

我想我刚刚回答了我的问题,但既然所有这些都已经写了

,你确实做到了:-) 如果需要添加一件事,那就是你应该小心 RuntimeExceptions,否则,你的 shutdownNow() 可能永远不会被调用。但你已经知道了,对吧?

I think I have just answered my question but since all this has been written

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文