CompletionService<wbr style="line-height:25px"><br style="line-height:25px"> java.util.concurrent<br style="line-height:25px"> 接口CompletionService<V><br style="line-height:25px"> 所有已知实现类:<br style="line-height:25px"> ExecutorCompletionService<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">它是将生产新的异步任务与使用已完成任务的结果分离开来的服务</span><wbr style="line-height:25px">。生产者submit任务。<br style="line-height:25px"> 使用者take已完成的任务,并按照<span style="line-height:25px"><wbr style="line-height:25px">完成这些任务的顺序</wbr></span><wbr style="line-height:25px">处理它们的结果。<br style="line-height:25px"> 例如,CompletionService可以用来管理异步IO,执行读操作的任务作为程序或系统的一部分提交,然后,当完成读操作时,会在程序的不同部分执行其他操作,执行操作的顺序可能与所请求的顺序不同。<br style="line-height:25px"> 通常,<wbr style="line-height:25px"><span style="color:#ff00ff; line-height:25px">CompletionService</span><span style="color:#003366; line-height:25px">依赖于一个单独的Executor来实际执行任务</span><wbr style="line-height:25px">,在这种情况下,CompletionService只管理一个<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">内部完成队列</span><wbr style="line-height:25px">。<br style="line-height:25px"> ExecutorCompletionService类提供了此方法的一个实现。<br style="line-height:25px"> 内存一致性效果:线程中向CompletionService提交任务之前的操作happen-before该任务执行的操作,<br style="line-height:25px"> 后者依次happen-before紧跟在从对应take()成功返回的操作。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">1:CompletionService只有一个实现类ExecutorCompletionService,具体参见《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/1710405792010975054817/" style="color:rgb(207,121,28); line-height:25px; text-decoration:underline">ExecutorCompletionService</a></strong>》<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">2:CompletionService的使用可以参见《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/1710405792010975054817/" style="color:rgb(207,121,28); line-height:25px; text-decoration:underline">ExecutorCompletionService</a></strong>》。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">3:CompletionService依赖于一个单独的Executor来实际执行任务。<br style="line-height:25px"><span style="line-height:25px">主要接口</span><br style="line-height:25px"> Future<V><wbr style="line-height:25px"><span style="color:#ff6600; line-height:25px">submit</span><wbr style="line-height:25px">(Callable<V>task)<br style="line-height:25px"> 提交要执行的值返回任务,并返回表示挂起的任务结果的Future。在完成时,可能会提取或轮询此任务。<br style="line-height:25px"> 参数:<br style="line-height:25px"> task-要提交的任务<br style="line-height:25px"> 返回:<br style="line-height:25px"> 一个表示挂起的任务完成的Future<br style="line-height:25px"> 抛出:<br style="line-height:25px"> RejectedExecutionException-如果不能安排要执行的任务<br style="line-height:25px"> NullPointerException-如果该任务为null<br style="line-height:25px"> Future<V><span style="color:#ff6600; line-height:25px"><wbr style="line-height:25px">submit</wbr></span><wbr style="line-height:25px">(Runnabletask,<br style="line-height:25px"> Vresult)<br style="line-height:25px"> 提交要执行的Runnable任务,并返回一个表示任务完成的Future,可以提取或轮询此任务。<br style="line-height:25px"> 参数:<br style="line-height:25px"> task-要提交的任务<br style="line-height:25px"> result- 任务成功完成情况下应该返回的结果<br style="line-height:25px"> 返回:<br style="line-height:25px"> 一个表示挂起的任务完成的Future,其get()方法将返回完成时给出的结果值<br style="line-height:25px"> 抛出:<br style="line-height:25px"> RejectedExecutionException-如果不能安排要执行的任务<br style="line-height:25px"> NullPointerException-如果该任务为null<br style="line-height:25px"> Future<V><wbr style="line-height:25px"><span style="color:#ff6600; line-height:25px">take</span><wbr style="line-height:25px">()<br style="line-height:25px"> throwsInterruptedException<br style="line-height:25px"> 获取并移除表示下一个已完成任务的Future,如果目前不存在这样的任务,则等待。<br style="line-height:25px"> 返回:<br style="line-height:25px"> 表示下一个已完成任务的Future<br style="line-height:25px"> 抛出:<br style="line-height:25px"> InterruptedException-如果在等待时被中断<br style="line-height:25px"> Future<V><wbr style="line-height:25px"><span style="color:#ff6600; line-height:25px">poll</span><wbr style="line-height:25px">()<br style="line-height:25px"> 获取并移除表示下一个已完成任务的Future,如果不存在这样的任务,则返回null。<br style="line-height:25px"> 返回:<br style="line-height:25px"> 表示下一个已完成任务的Future;如果不存在这样的任务,则返回null<br style="line-height:25px"> Future<V>poll(longtimeout,TimeUnitunit)<br style="line-height:25px"> throwsInterruptedException<br style="line-height:25px"><br style="line-height:25px"> 获取并移除表示下一个已完成任务的Future,如果目前不存在这样的任务,则将等待指定的时间(如果有必要)。<br style="line-height:25px"> 参数:<br style="line-height:25px"> timeout-放弃之前需要等待的时间长度,以unit为时间单位<br style="line-height:25px"> unit-确定如何解释timeout参数的TimeUnit<br style="line-height:25px"> 返回:<br style="line-height:25px"> 表示下一个已完成任务的Future;如果等待了指定时间仍然不存在这样的任务,则返回null<br style="line-height:25px"> 抛出:<br style="line-height:25px"> InterruptedException-如果在等待时被中断</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
CompletionService
最新推荐文章于 2025-06-23 11:28:36 发布