Clover Coverage Report - Backport Util Concurrent v3.0
Coverage timestamp: Fri May 9 2008 11:05:23 EST
../../../../../../../img/srcFileCovDistChart9.png 35% of files have more coverage
32   175   18   2.91
16   65   0.56   5.5
11     1.64  
2    
 
  ExecutorCompletionService       Line # 81 29 16 79.6% 0.7962963
  ExecutorCompletionService.QueueingFuture       Line # 89 3 2 100% 1.0
 
  (40)
 
1    /*
2    * Written by Doug Lea with assistance from members of JCP JSR-166
3    * Expert Group and released to the public domain, as explained at
4    * http://creativecommons.org/licenses/publicdomain
5    */
6   
7    package edu.emory.mathcs.backport.java.util.concurrent;
8    import edu.emory.mathcs.backport.java.util.concurrent.*; // for javadoc (till 6280605 is fixed)
9   
10    /**
11    * A {@link CompletionService} that uses a supplied {@link Executor}
12    * to execute tasks. This class arranges that submitted tasks are,
13    * upon completion, placed on a queue accessible using <tt>take</tt>.
14    * The class is lightweight enough to be suitable for transient use
15    * when processing groups of tasks.
16    *
17    * <p>
18    *
19    * <b>Usage Examples.</b>
20    *
21    * Suppose you have a set of solvers for a certain problem, each
22    * returning a value of some type <tt>Result</tt>, and would like to
23    * run them concurrently, processing the results of each of them that
24    * return a non-null value, in some method <tt>use(Result r)</tt>. You
25    * could write this as:
26    *
27    * <pre>
28    * void solve(Executor e,
29    * Collection&lt;Callable&lt;Result&gt;&gt; solvers)
30    * throws InterruptedException, ExecutionException {
31    * CompletionService&lt;Result&gt; ecs
32    * = new ExecutorCompletionService&lt;Result&gt;(e);
33    * for (Callable&lt;Result&gt; s : solvers)
34    * ecs.submit(s);
35    * int n = solvers.size();
36    * for (int i = 0; i &lt; n; ++i) {
37    * Result r = ecs.take().get();
38    * if (r != null)
39    * use(r);
40    * }
41    * }
42    * </pre>
43    *
44    * Suppose instead that you would like to use the first non-null result
45    * of the set of tasks, ignoring any that encounter exceptions,
46    * and cancelling all other tasks when the first one is ready:
47    *
48    * <pre>
49    * void solve(Executor e,
50    * Collection&lt;Callable&lt;Result&gt;&gt; solvers)
51    * throws InterruptedException {
52    * CompletionService&lt;Result&gt; ecs
53    * = new ExecutorCompletionService&lt;Result&gt;(e);
54    * int n = solvers.size();
55    * List&lt;Future&lt;Result&gt;&gt; futures
56    * = new ArrayList&lt;Future&lt;Result&gt;&gt;(n);
57    * Result result = null;
58    * try {
59    * for (Callable&lt;Result&gt; s : solvers)
60    * futures.add(ecs.submit(s));
61    * for (int i = 0; i &lt; n; ++i) {
62    * try {
63    * Result r = ecs.take().get();
64    * if (r != null) {
65    * result = r;
66    * break;
67    * }
68    * } catch (ExecutionException ignore) {}
69    * }
70    * }
71    * finally {
72    * for (Future&lt;Result&gt; f : futures)
73    * f.cancel(true);
74    * }
75    *
76    * if (result != null)
77    * use(result);
78    * }
79    * </pre>
80    */
 
81    public class ExecutorCompletionService implements CompletionService {
82    private final Executor executor;
83    private final AbstractExecutorService aes;
84    private final BlockingQueue completionQueue;
85   
86<