简体   繁体   English

Java ExecutorService何时调用shutdown()

[英]Java ExecutorService when to call shutdown()

I'm new to Java multithreading. 我是Java多线程的新手。 I'm using ExecutorService to download a bunch of web pages to analyze. 我正在使用ExecutorService下载一堆网页进行分析。 Currently my approach is to submit all Callables and put the Future objects in a list in a loop, then call shutdown() and awaitTermination() , finally I process the future list. 当前,我的方法是提交所有Callables并将Future对象放入循环中的列表中,然后调用shutdown()awaitTermination() ,最后我处理awaitTermination()列表。
My question is in my case, is it OK to put shutdown() after filling up the future list like this? 我的问题是我的情况,这样填写以后的列表后可以将shutdown()放下吗? As get() of Future already blocks until the task is done, seems that I should instead call it when I finish processing all the futures. 由于Future的get()在执行任务之前一直处于阻塞状态,因此当我处理完所有期货时,似乎应该改为调用它。

Note the javadoc of shutdown() 注意shutdown()的Javadoc

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. 启动有序关闭,在该关闭中执行先前提交的任务,但不接受任何新任务。 Invocation has no additional effect if already shut down. 如果已关闭,则调用不会产生任何其他影响。

This is a clean stop. 这是一个干净的站。 The submitted tasks will run until they are done. 提交的任务将一直运行到完成为止。 Since you also have awaitTermination() which 由于您还有awaitTermination()

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first. 阻塞直到关闭请求后所有任务完成执行,或者发生超时,或者当前线程被中断(以先发生的为准)。

Your Future#get() method calls will return immediately (if the awaitTermination() didn't time out). 您的Future#get()方法调用将立即返回(如果awaitTermination()没有超时)。 There is no issue with what you are doing. 您所做的没有问题。


Make sure your tasks don't run forever. 确保您的任务不会永远运行。

There are two methods 有两种方法

1. shutdown()

as @Sotirios said, thread will completes the current tasks but did not add any new task for execution. 正如@Sotirios所说,线程将完成当前任务,但不会添加任何新任务来执行。 So the current tasks will run to finish 因此,当前任务将运行完成

2. shutdownNow()

It suddenly terminates all the tasks. 它突然终止了所有任务。

So by using the shutdown() method you can check weather all tasks are finished or not and can wait until all tasks are finished. 因此,通过使用shutdown()方法,您可以检查天气所有任务是否完成,并可以等待所有任务完成。 To acheive what you want, you can use isTerminated() method in the infinite loop as 要实现所需的功能,可以在无限循环中使用isTerminated()方法,如下所示:

while(!isTerminated());

So when all the finished next sequential action will be taken. 因此,当所有完成的下一个顺序动作都将采取时。

There is no point using a thread pool if you shut it down after you've used it. 如果在使用线程后将其关闭,则没有用处。 It's designed to allow you to reuse the threads. 它旨在允许您重用线程。

Instead, put the the Executor in a static field (or better, inject it with a DI framework like Spring). 取而代之的是,将Executor放在一个静态字段中(或者更好的是,将其注入一个像Spring这样的DI框架)。 Then use java.util.concurrent.ExecutorService.invokeAll(Collection<? extends Callable<T>>) , which blocks until all the tasks you submitted have completed. 然后使用java.util.concurrent.ExecutorService.invokeAll(Collection<? extends Callable<T>>)阻塞,直到您提交的所有任务都完成为止。

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

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