简体   繁体   English

如何使用ExecutorCompletionService

[英]How to use an ExecutorCompletionService

I would like to use the constructor on the ExecutorCompletionService which takes a pre-defined BlockingQueue . 我想在带有预定义BlockingQueueExecutorCompletionService上使用构造函数。

Can anyone provide a code snippet to show how this is done. 任何人都可以提供代码片段来说明如何完成此操作。 It seems only to take aa BlockingQueue with Future s. 似乎只将Future用作BlockingQueue How does this match with the fact that submit must be done with Callable s. 这与必须使用Callable完成提交的事实如何匹配?

I'm getting a little confused here - please help... 我在这里有点困惑-请帮助...

ExecutorCompletionServiceJavadoc中包含一个大型代码示例,而记录在案的构造函数采用BlockingQueue

The Callables supplied to submit() will be wrapped up internally as FutureTask s, and it's those (or a wrapper around one) that will eventually be inserted into the provided queue. 提供给submit()Callables将作为FutureTask在内部进行包装,最终将这些(或一个包装)插入到提供的队列中。 That's all an implementation detail, though. 不过,这只是实现细节。 (And, note, that this use case of inserting the outcome into the queue is the main reason why there's a protected done() method in FutureTask .) The contract says that result-yielding functions go in, and eventually results come out. (而且,请注意,在FutureTask使用受保护的done()方法的主要原因是将结果插入队列中的这种用例。)合同规定,将使用产生结果的函数,最终结果出来。 The Future s drawn from the queue represent the eventual outcomes of the functions you submitted earlier. 从队列中提取的Future代表您之前提交的功能的最终结果。

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

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