1   /*
2    * %W% %E%
3    *
4    * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
5    * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6    */
7   
8   package java.util.concurrent;
9   
10  /**
11   * A service that decouples the production of new asynchronous tasks
12   * from the consumption of the results of completed tasks.  Producers
13   * <tt>submit</tt> tasks for execution. Consumers <tt>take</tt>
14   * completed tasks and process their results in the order they
15   * complete.  A <tt>CompletionService</tt> can for example be used to
16   * manage asynchronous IO, in which tasks that perform reads are
17   * submitted in one part of a program or system, and then acted upon
18   * in a different part of the program when the reads complete,
19   * possibly in a different order than they were requested.
20   *
21   * <p>Typically, a <tt>CompletionService</tt> relies on a separate
22   * {@link Executor} to actually execute the tasks, in which case the
23   * <tt>CompletionService</tt> only manages an internal completion
24   * queue. The {@link ExecutorCompletionService} class provides an
25   * implementation of this approach.
26   *
27   * <p>Memory consistency effects: Actions in a thread prior to
28   * submitting a task to a {@code CompletionService}
29   * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
30   * actions taken by that task, which in turn <i>happen-before</i>
31   * actions following a successful return from the corresponding {@code take()}.
32   *
33   */
34  public interface CompletionService<V> {
35      /**
36       * Submits a value-returning task for execution and returns a Future
37       * representing the pending results of the task.  Upon completion,
38       * this task may be taken or polled.
39       *
40       * @param task the task to submit
41       * @return a Future representing pending completion of the task
42       * @throws RejectedExecutionException if the task cannot be
43       *         scheduled for execution
44       * @throws NullPointerException if the task is null
45       */
46      Future<V> submit(Callable<V> task);
47  
48      /**
49       * Submits a Runnable task for execution and returns a Future
50       * representing that task.  Upon completion, this task may be
51       * taken or polled.
52       *
53       * @param task the task to submit
54       * @param result the result to return upon successful completion
55       * @return a Future representing pending completion of the task,
56       *         and whose <tt>get()</tt> method will return the given
57       *         result value upon completion
58       * @throws RejectedExecutionException if the task cannot be
59       *         scheduled for execution
60       * @throws NullPointerException if the task is null
61       */
62      Future<V> submit(Runnable task, V result);
63  
64      /**
65       * Retrieves and removes the Future representing the next
66       * completed task, waiting if none are yet present.
67       *
68       * @return the Future representing the next completed task
69       * @throws InterruptedException if interrupted while waiting
70       */
71      Future<V> take() throws InterruptedException;
72  
73  
74      /**
75       * Retrieves and removes the Future representing the next
76       * completed task or <tt>null</tt> if none are present.
77       *
78       * @return the Future representing the next completed task, or
79       *         <tt>null</tt> if none are present
80       */
81      Future<V> poll();
82  
83      /**
84       * Retrieves and removes the Future representing the next
85       * completed task, waiting if necessary up to the specified wait
86       * time if none are yet present.
87       *
88       * @param timeout how long to wait before giving up, in units of
89       *        <tt>unit</tt>
90       * @param unit a <tt>TimeUnit</tt> determining how to interpret the
91       *        <tt>timeout</tt> parameter
92       * @return the Future representing the next completed task or
93       *         <tt>null</tt> if the specified waiting time elapses
94       *         before one is present
95       * @throws InterruptedException if interrupted while waiting
96       */
97      Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException;
98  }
99