简体   繁体   English

使用executor.shutdown()

[英]The use of executor.shutdown()

Look at the following piece of code: 看下面这段代码:

     public void pinger()
     {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
        executor.scheduleAtFixedRate(runnable, start, rate, TimeUnit.SECONDS);
        executor.shutdown();
     }

Is there any use of writing the shutdown command in this case? 在这种情况下是否有任何写入shutdown命令的用法? Different clients would create their own runnable objects and invoke this function. 不同的客户端将创建自己的可运行对象并调用此函数。

When you shutdown an executor, no new task will be accepted. 关闭执行程序时,不会接受任何新任务。 Since you create a new one inside the pinger method every task has its own executor. 由于您在pinger方法中创建了一个新任务,因此每个任务都有自己的执行程序。 A shutdown as you write will only free resource once the corrent task is terminated. 写入时关闭只会在更正corrent任务后释放资源。

Some notes: 一些说明:

  1. You should not create Executor for each client request. 您不应为每个客户端请求创建Executor
  2. Create Executor out side of client request and submit tasks to Executor 在客户端请求之外创建Executor并将任务提交给Executor
  3. When you decide that you should not accept new tasks to Executor , then shutdown the executor. 当您决定不接受Executor新任务时,请关闭执行程序。 The right way of shutting down Executor is explained in below post: 关闭Executor的正确方法在下面的帖子中解释:

How to forcefully shutdown java ExecutorService 如何强制关闭java ExecutorService

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM