Java CompletionService接口总结 CompletionService接口注释翻译和解析中英文对照版

CompletionService接口源码重点

  1. CompletionService接口是用于将新异步任务的创建与获取已完成任务的结果分离的服务,生产者submit要执行的任务。使用者take获取已完成的任务的Future,并按完成的顺序处理其结果
  2. 可以调用Future的get方法获取任务执行的结果
  3. take是阻塞的方法阻塞直到获取一个已完成任务的Future,而poll方法是非阻塞的,如果此时没有任务完成,则直接返回null,poll(long timeout, TimeUnit unit)是会超时的阻塞,即阻塞给定超时时间,如果超时则返回null,在超时时间内有任务完成返回Future
  4. 内存一致性:任务提交submit先于该任务被执行,而该任务被执行又先于take获取结果

CompletionService接口方法

方法名作用
Future submit(Callable task);提交一个任务以供执行,Callable里面的call方法就是要执行的任务,并返回一个Future用于获取任务执行结果,结果是call方法返回的结果
Future submit(Runnable task, V result)提交一个任务以供执行,Runnable里面的run方法就是要执行的任务, 并返回一个Future用于获取任务执行结果,执行结果是传入的 result
Future take()获取并删除表示下一个已完成任务的Future,如果还没有,则阻塞等待
Future poll()获取并删除表示下一个已完成任务的Future,不阻塞,如果没有返回null
Future poll(long timeout, TimeUnit unit)获取并删除表示下一个已完成任务的Future,只阻塞给定的超时时间,如果超时返回null

CompletionService接口源码

package java.util.concurrent;

/**
 * A service that decouples the production of new asynchronous tasks
 * from the consumption of the results of completed tasks.  Producers
 * {@code submit} tasks for execution. Consumers {@code take}
 * completed tasks and process their results in the order they
 * complete.  A {@code CompletionService} can for example be used to
 * manage asynchronous I/O, in which tasks that perform reads are
 * submitted in one part of a program or system, and then acted upon
 * in a different part of the program when the reads complete,
 * possibly in a different order than they were requested.
 * 将新异步任务的生成与已完成任务的结果的使用分离的服务。
 * 生产者submit要执行的任务。使用者take已完成的任务,并按完成的顺序处理其结果。
 * 例如,CompletionService可用于管理异步I/O,在异步I/O中,
 * 执行读取的任务在程序或系统的一个部分中提交,
 * 然后在读取完成时在程序的另一个部分中执行,可能与请求的顺序不同。
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 *
 * <p>Typically, a {@code CompletionService} relies on a separate
 * {@link Executor} to actually execute the tasks, in which case the
 * {@code CompletionService} only manages an internal completion
 * queue. The {@link ExecutorCompletionService} class provides an
 * implementation of this approach.
 * 
 * 通常,CompletionService依赖单独的Executor来实际执行任务,
 * 在这种情况下,CompletionService只管理内部完成队列。
 * ExecutionCompletionService类提供了这种方法的实现。
 * 
 * 
 *
 * <p>Memory consistency effects: Actions in a thread prior to
 * submitting a task to a {@code CompletionService}
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
 * actions taken by that task, which in turn <i>happen-before</i>
 * actions following a successful return from the corresponding {@code take()}.
 * 内存一致性影响:在将任务提交给CompletionService之前,
 * 线程中的操作发生在该任务执行的操作之前,
 * 而该任务执行的操作又发生在从相应的take()成功返回后的操作之前。
 * 即提交先于该任务被执行,而该任务被执行又先于take获取结果
 * 
 *
 * @since 1.5
 */
public interface CompletionService<V> {
    /**
     * Submits a value-returning task for execution and returns a Future
     * representing the pending results of the task.  Upon completion,
     * this task may be taken or polled.
     * 提交一个返回值的任务以供执行,并返回一个表示任务挂起结果的未来。
     * 完成后,可以执行或轮询此任务。
     *
     * @param task the task to submit 要提交的任务
     * @return a Future representing pending completion of the task 表示待完成任务的未来
     * @throws RejectedExecutionException if the task cannot be
     *         scheduled for execution 如果无法安排任务执行
     * @throws NullPointerException if the task is null 如果任务为空
     */
    Future<V> submit(Callable<V> task);

    /**
     * Submits a Runnable task for execution and returns a Future
     * representing that task.  Upon completion, this task may be
     * taken or polled.
     * 提交可运行任务以执行,并返回表示该任务的Future。
     * 完成后,可以执行或轮询此任务。
     *
     * @param task the task to submit 要提交的任务 
     * @param result the result to return upon successful completion 成功完成后返回的结果
     * @return a Future representing pending completion of the task,
     *         and whose {@code get()} method will return the given
     *         result value upon completion
     *         表示待完成任务的Future,其get()方法将在完成时返回给定的结果值
     * 
     * @throws RejectedExecutionException if the task cannot be
     *         scheduled for execution 如果无法安排任务执行
     * 
     * @throws NullPointerException if the task is null
     */
    Future<V> submit(Runnable task, V result);

    /**
     * Retrieves and removes the Future representing the next
     * completed task, waiting if none are yet present.
     * 检索并删除表示下一个已完成任务的未来,如果还没有,则等待。
     *
     * @return the Future representing the next completed task 表示下一个已完成任务的未来
     * @throws InterruptedException if interrupted while waiting 如果在等待时被中断
     */
    Future<V> take() throws InterruptedException;

    /**
     * Retrieves and removes the Future representing the next
     * completed task, or {@code null} if none are present.
     * 检索并删除表示下一个已完成任务的未来,或null(如果不存在)。
     *
     * @return the Future representing the next completed task, or
     *         {@code null} if none are present
     *         表示下一个已完成任务的Future,如果不存在,则为null
     */
    Future<V> poll();

    /**
     * Retrieves and removes the Future representing the next
     * completed task, waiting if necessary up to the specified wait
     * time if none are yet present.
     * 检索并删除表示下一个已完成任务的未来,如果没有,
     * 则在必要时等待到指定的等待时间。
     *
     * @param timeout how long to wait before giving up, in units of
     *        {@code unit} 放弃前等待多长时间,单位为unit
     * @param unit a {@code TimeUnit} determining how to interpret the
     *        {@code timeout} parameter 确定如何解释timeout参数的TimeUnit
     * @return the Future representing the next completed task or
     *         {@code null} if the specified waiting time elapses
     *         before one is present
     *         表示下一个已完成任务的未来或null(如果指定的等待时间在任务出现之前已过)
     * 
     * @throws InterruptedException if interrupted while waiting 如果在等待时被中断
     */
    Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lolxxs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值