确定ExecutorService的关闭时间

发布于 2024-12-05 16:53:49 字数 421 浏览 0 评论 0原文

我有一个 ExecutorService 来处理一些任务。客户端线程可以调用ExecutorService上的shutdown()。我想在 ExecutorService 完全关闭后运行一些清理代码。是否有一种机制可以在 ExecutorService 完成关闭后运行回调方法。

注意:

  1. 无法调用shutdownNow()
  2. 清理代码必须shutdown()完成后运行。
  3. ExecutorService 是一个 newCachedThreadPoolExecutor()

I have an ExecutorService that processes some tasks. The client threads can call shutdown() on the ExecutorService. I want to run some cleanup code after the ExecutorService has completely shutdown. Is there a mechanism to run a callback method after the ExecutorService has completed its shutdown.

NOTE:

  1. I cannot call shutdownNow()
  2. The clean-up code must run after the shutdown() is completed.
  3. The ExecutorService is a newCachedThreadPoolExecutor();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

负佳期 2024-12-12 16:53:49

启动另一个线程/executor-managed-runnable来检查ExecutorService.awaitTermination(...) 在循环中的 try-catch 语句中直到返回 true,然后运行关闭处理代码。该循环是必要的,因为该方法在中断时可能会过早返回。

像这样的东西:

public class ShutdownHandler implements Runnable {

    private final ExecutorService service;

    public ShutdownHandler(final ExecutorService service) {
        this.service = service;
    }

    @Override
    public void run() {
        boolean terminated = false;
        while (!terminated) {
            try {
                terminated = service.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
            } catch (final InterruptedException ex) {
                // check again until terminated
            }
        }

        // your shutdown handling code here
    }
}

Start another thread/executor-managed-runnable that checks ExecutorService.awaitTermination(...) in a try-catch statement in a loop until it returns true, then run your shutdown handling code. The loop is necessary because the method might return prematurely when interrupted.

Something like this:

public class ShutdownHandler implements Runnable {

    private final ExecutorService service;

    public ShutdownHandler(final ExecutorService service) {
        this.service = service;
    }

    @Override
    public void run() {
        boolean terminated = false;
        while (!terminated) {
            try {
                terminated = service.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
            } catch (final InterruptedException ex) {
                // check again until terminated
            }
        }

        // your shutdown handling code here
    }
}
[旋木] 2024-12-12 16:53:49

我会扩展并覆盖:

public class MyExecutorService extends ThreadPoolExecutor {
    @Override
    public void shutdown() {
        super.shutdown();
        // do what you need to do here
    }
}

类似的东西

I would extends and override:

public class MyExecutorService extends ThreadPoolExecutor {
    @Override
    public void shutdown() {
        super.shutdown();
        // do what you need to do here
    }
}

Something like that

葬シ愛 2024-12-12 16:53:49

好吧,如果您调用 executorService.invokeAll(myListOfCallableTasks) 和 executorService.shutDown() 调用的线程将阻塞,直到所有任务完成:

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html#invokeAll(java.util.Collection)

Well, if you call executorService.invokeAll(myListOfCallableTasks) and them executorService.shutDown() the thread calling that will block until all the tasks are finished:

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html#invokeAll(java.util.Collection)

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