ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ExecutorCompletionService.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/ExecutorCompletionService.java (file contents):
Revision 1.17 by dl, Sat Dec 2 20:53:11 2006 UTC vs.
Revision 1.18 by jsr166, Tue Sep 11 17:57:19 2007 UTC

# Line 9 | Line 9 | package java.util.concurrent;
9   /**
10   * A {@link CompletionService} that uses a supplied {@link Executor}
11   * to execute tasks.  This class arranges that submitted tasks are,
12 < * upon completion, placed on a queue accessible using <tt>take</tt>.
12 > * upon completion, placed on a queue accessible using {@code take}.
13   * The class is lightweight enough to be suitable for transient use
14   * when processing groups of tasks.
15   *
# Line 18 | Line 18 | package java.util.concurrent;
18   * <b>Usage Examples.</b>
19   *
20   * Suppose you have a set of solvers for a certain problem, each
21 < * returning a value of some type <tt>Result</tt>, and would like to
21 > * returning a value of some type {@code Result}, and would like to
22   * run them concurrently, processing the results of each of them that
23 < * return a non-null value, in some method <tt>use(Result r)</tt>. You
23 > * return a non-null value, in some method {@code use(Result r)}. You
24   * could write this as:
25   *
26 < * <pre>
27 < *   void solve(Executor e,
28 < *              Collection&lt;Callable&lt;Result&gt;&gt; solvers)
26 > * <pre> {@code
27 > * void solve(Executor e,
28 > *            Collection<Callable<Result>> solvers)
29   *     throws InterruptedException, ExecutionException {
30 < *       CompletionService&lt;Result&gt; ecs
31 < *           = new ExecutorCompletionService&lt;Result&gt;(e);
32 < *       for (Callable&lt;Result&gt; s : solvers)
33 < *           ecs.submit(s);
34 < *       int n = solvers.size();
35 < *       for (int i = 0; i &lt; n; ++i) {
36 < *           Result r = ecs.take().get();
37 < *           if (r != null)
38 < *               use(r);
39 < *       }
40 < *   }
41 < * </pre>
30 > *     CompletionService<Result> ecs
31 > *         = new ExecutorCompletionService<Result>(e);
32 > *     for (Callable<Result> s : solvers)
33 > *         ecs.submit(s);
34 > *     int n = solvers.size();
35 > *     for (int i = 0; i < n; ++i) {
36 > *         Result r = ecs.take().get();
37 > *         if (r != null)
38 > *             use(r);
39 > *     }
40 > * }}</pre>
41   *
42   * Suppose instead that you would like to use the first non-null result
43   * of the set of tasks, ignoring any that encounter exceptions,
44   * and cancelling all other tasks when the first one is ready:
45   *
46 < * <pre>
47 < *   void solve(Executor e,
48 < *              Collection&lt;Callable&lt;Result&gt;&gt; solvers)
46 > * <pre> {@code
47 > * void solve(Executor e,
48 > *            Collection<Callable<Result>> solvers)
49   *     throws InterruptedException {
50 < *       CompletionService&lt;Result&gt; ecs
51 < *           = new ExecutorCompletionService&lt;Result&gt;(e);
52 < *       int n = solvers.size();
53 < *       List&lt;Future&lt;Result&gt;&gt; futures
54 < *           = new ArrayList&lt;Future&lt;Result&gt;&gt;(n);
55 < *       Result result = null;
56 < *       try {
57 < *           for (Callable&lt;Result&gt; s : solvers)
58 < *               futures.add(ecs.submit(s));
59 < *           for (int i = 0; i &lt; n; ++i) {
60 < *               try {
61 < *                   Result r = ecs.take().get();
62 < *                   if (r != null) {
63 < *                       result = r;
64 < *                       break;
65 < *                   }
66 < *               } catch (ExecutionException ignore) {}
67 < *           }
68 < *       }
69 < *       finally {
70 < *           for (Future&lt;Result&gt; f : futures)
71 < *               f.cancel(true);
72 < *       }
73 < *
74 < *       if (result != null)
75 < *           use(result);
76 < *   }
78 < * </pre>
50 > *     CompletionService<Result> ecs
51 > *         = new ExecutorCompletionService<Result>(e);
52 > *     int n = solvers.size();
53 > *     List<Future<Result>> futures
54 > *         = new ArrayList<Future<Result>>(n);
55 > *     Result result = null;
56 > *     try {
57 > *         for (Callable<Result> s : solvers)
58 > *             futures.add(ecs.submit(s));
59 > *         for (int i = 0; i < n; ++i) {
60 > *             try {
61 > *                 Result r = ecs.take().get();
62 > *                 if (r != null) {
63 > *                     result = r;
64 > *                     break;
65 > *                 }
66 > *             } catch (ExecutionException ignore) {}
67 > *         }
68 > *     }
69 > *     finally {
70 > *         for (Future<Result> f : futures)
71 > *             f.cancel(true);
72 > *     }
73 > *
74 > *     if (result != null)
75 > *         use(result);
76 > * }}</pre>
77   */
78   public class ExecutorCompletionService<V> implements CompletionService<V> {
79      private final Executor executor;
# Line 114 | Line 112 | public class ExecutorCompletionService<V
112       * {@link LinkedBlockingQueue} as a completion queue.
113       *
114       * @param executor the executor to use
115 <     * @throws NullPointerException if executor is <tt>null</tt>
115 >     * @throws NullPointerException if executor is {@code null}
116       */
117      public ExecutorCompletionService(Executor executor) {
118          if (executor == null)
# Line 132 | Line 130 | public class ExecutorCompletionService<V
130       *
131       * @param executor the executor to use
132       * @param completionQueue the queue to use as the completion queue
133 <     * normally one dedicated for use by this service. This queue is
134 <     * treated as unbounded -- failed attempted <tt>Queue.add</tt>
135 <     * operations for completed taskes cause them not to be
136 <     * retrievable.
137 <     * @throws NullPointerException if executor or completionQueue are <tt>null</tt>
133 >     *        normally one dedicated for use by this service. This
134 >     *        queue is treated as unbounded -- failed attempted
135 >     *        {@code Queue.add} operations for completed taskes cause
136 >     *        them not to be retrievable.
137 >     * @throws NullPointerException if executor or completionQueue are {@code null}
138       */
139      public ExecutorCompletionService(Executor executor,
140                                       BlockingQueue<Future<V>> completionQueue) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines