我要大量上传到某个服务器 - 我的程序每次上传大约1秒钟 - 但我每次都会上传大约1万个文档。 为此,我想使用并行性来及时发送文档。 (我的服务器已经同时接受了很多请求。)
所以,我创建了一个上传文档的Task(实现Callable)。 并将其放在ExecutorService中。
ExecutorUpload.java
public void execute(){
ExecutorService executor = Executors.newWorkStealingPool();
//this code create the InputStream objects from
//real Files from disk using java.io.InputStream and java.io.File
List<CallbackCreateDocumentTask> tasks = createTasks(...);
//this list holds the Future objects to try to terminate the executorService
List<Future<DocumentDTO>> futures = new CopyOnWriteArrayList<>();
//iterate the list and call the Tasks
tasks.stream().forEach((task) -> futures.add(executor.submit(task)));
executor.shutdown();
//here i was trying to stop the executor,
//but this makes me lose de asynchronous upload because
//the application stops to wait this unique executoService to terminate,
//and i've more than one executorService doing upload
//executor.awaitTermination(10, TimeUnit.MINUTES);
}
App.java
public static void main (String[] args){
new ExecutorUpload().execute();
}
这段代码很好。在我创建的所有ExecutorService实例中,我的所有文档都由Tasks上传。 但是,我的申请永远不会结束。它就像是在等待更多未完成的执行器服务;
有人知道为什么我的申请永远不会结束吗? 我怀疑有一些事情: