ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/AbstractExecutorService.java
Revision: 1.13
Committed: Sun Dec 21 12:24:48 2003 UTC (20 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.12: +9 -0 lines
Log Message:
Documentation improvments; support two-arg submit

File Contents

# User Rev Content
1 tim 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain. Use, modify, and
4     * redistribute this code in any way without acknowledgement.
5     */
6    
7     package java.util.concurrent;
8    
9 dl 1.3 import java.util.*;
10 tim 1.1
11     /**
12 dl 1.2 * Provides default implementation of {@link ExecutorService}
13 dl 1.12 * execution methods. This class implements the <tt>submit</tt>,
14     * <tt>invokeAny</tt> and <tt>invokeAll</tt> methods using the default
15     * {@link FutureTask} class provided in this package. For example,
16     * the the implementation of <tt>submit(Runnable)</tt> creates an
17     * associated <tt>FutureTask</tt> that is executed and
18 dl 1.2 * returned. Subclasses overriding these methods to use different
19     * {@link Future} implementations should do so consistently for each
20     * of these methods.
21 tim 1.1 *
22     * @since 1.5
23     * @author Doug Lea
24     */
25     public abstract class AbstractExecutorService implements ExecutorService {
26    
27 dl 1.11 public Future<?> submit(Runnable task) {
28 dl 1.13 if (task == null) throw new NullPointerException();
29 dl 1.11 FutureTask<Object> ftask = new FutureTask<Object>(task, null);
30 tim 1.1 execute(ftask);
31     return ftask;
32     }
33    
34 dl 1.13 public <T> Future<T> submit(Runnable task, T result) {
35     if (task == null) throw new NullPointerException();
36     FutureTask<T> ftask = new FutureTask<T>(task, result);
37     execute(ftask);
38     return ftask;
39     }
40    
41 tim 1.1 public <T> Future<T> submit(Callable<T> task) {
42 dl 1.13 if (task == null) throw new NullPointerException();
43 tim 1.1 FutureTask<T> ftask = new FutureTask<T>(task);
44     execute(ftask);
45     return ftask;
46     }
47    
48 dl 1.7 // any/all methods, each a little bit different than the other
49 dl 1.3
50    
51 dl 1.7 public <T> T invokeAny(Collection<Callable<T>> tasks)
52     throws InterruptedException, ExecutionException {
53 dl 1.3 if (tasks == null)
54     throw new NullPointerException();
55     int n = tasks.size();
56     if (n == 0)
57 dl 1.7 throw new IllegalArgumentException();
58     List<Future<T>> futures= new ArrayList<Future<T>>(n);
59     ExecutorCompletionService<T> ecs =
60     new ExecutorCompletionService<T>(this);
61 dl 1.3 try {
62 dl 1.7 for (Callable<T> t : tasks)
63     futures.add(ecs.submit(t));
64     ExecutionException ee = null;
65     RuntimeException re = null;
66     while (n-- > 0) {
67     Future<T> f = ecs.take();
68     try {
69     return f.get();
70     } catch(ExecutionException eex) {
71     ee = eex;
72     } catch(RuntimeException rex) {
73     re = rex;
74     }
75     }
76     if (ee != null)
77     throw ee;
78     if (re != null)
79     throw new ExecutionException(re);
80     throw new ExecutionException();
81 dl 1.3 } finally {
82 dl 1.6 for (Future<T> f : futures)
83 dl 1.4 f.cancel(true);
84 dl 1.3 }
85     }
86    
87 dl 1.7 public <T> T invokeAny(Collection<Callable<T>> tasks,
88     long timeout, TimeUnit unit)
89     throws InterruptedException, ExecutionException, TimeoutException {
90 dl 1.3 if (tasks == null || unit == null)
91     throw new NullPointerException();
92 dl 1.7 long nanos = unit.toNanos(timeout);
93 dl 1.3 int n = tasks.size();
94     if (n == 0)
95 dl 1.7 throw new IllegalArgumentException();
96     List<Future<T>> futures= new ArrayList<Future<T>>(n);
97     ExecutorCompletionService<T> ecs =
98     new ExecutorCompletionService<T>(this);
99 dl 1.3 try {
100 dl 1.7 for (Callable<T> t : tasks)
101     futures.add(ecs.submit(t));
102     ExecutionException ee = null;
103     RuntimeException re = null;
104     long lastTime = System.nanoTime();
105     while (n-- > 0) {
106     Future<T> f = ecs.poll(nanos, TimeUnit.NANOSECONDS);
107 dl 1.8 if (f == null)
108     throw new TimeoutException();
109 dl 1.7 try {
110     return f.get();
111     } catch(ExecutionException eex) {
112     ee = eex;
113     } catch(RuntimeException rex) {
114     re = rex;
115     }
116 dl 1.8 long now = System.nanoTime();
117     nanos -= now - lastTime;
118     lastTime = now;
119 dl 1.7 }
120     if (ee != null)
121     throw ee;
122     if (re != null)
123     throw new ExecutionException(re);
124     throw new ExecutionException();
125 dl 1.3 } finally {
126 dl 1.6 for (Future<T> f : futures)
127 dl 1.4 f.cancel(true);
128 dl 1.3 }
129     }
130    
131 dl 1.9 public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks)
132 dl 1.3 throws InterruptedException {
133     if (tasks == null)
134     throw new NullPointerException();
135 dl 1.6 List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
136     boolean done = false;
137 dl 1.3 try {
138     for (Callable<T> t : tasks) {
139 dl 1.6 FutureTask<T> f = new FutureTask<T>(t);
140 dl 1.3 futures.add(f);
141     execute(f);
142     }
143 dl 1.6 for (Future<T> f : futures) {
144     if (!f.isDone()) {
145     try {
146     f.get();
147     } catch(CancellationException ignore) {
148     } catch(ExecutionException ignore) {
149     }
150     }
151     }
152     done = true;
153 dl 1.3 return futures;
154     } finally {
155 dl 1.6 if (!done)
156 dl 1.4 for (Future<T> f : futures)
157     f.cancel(true);
158 dl 1.3 }
159     }
160    
161 dl 1.9 public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks,
162 dl 1.6 long timeout, TimeUnit unit)
163 dl 1.3 throws InterruptedException {
164     if (tasks == null || unit == null)
165     throw new NullPointerException();
166     long nanos = unit.toNanos(timeout);
167 dl 1.6 List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
168     boolean done = false;
169 dl 1.3 try {
170     for (Callable<T> t : tasks) {
171 dl 1.6 FutureTask<T> f = new FutureTask<T>(t);
172 dl 1.3 futures.add(f);
173     execute(f);
174     }
175 dl 1.6 long lastTime = System.nanoTime();
176     for (Future<T> f : futures) {
177     if (!f.isDone()) {
178     if (nanos < 0)
179     return futures;
180     try {
181     f.get(nanos, TimeUnit.NANOSECONDS);
182     } catch(CancellationException ignore) {
183     } catch(ExecutionException ignore) {
184     } catch(TimeoutException toe) {
185     return futures;
186     }
187 dl 1.8 long now = System.nanoTime();
188     nanos -= now - lastTime;
189     lastTime = now;
190 dl 1.6 }
191     }
192     done = true;
193 dl 1.3 return futures;
194     } finally {
195 dl 1.6 if (!done)
196 dl 1.4 for (Future<T> f : futures)
197     f.cancel(true);
198 dl 1.3 }
199     }
200    
201 tim 1.1 }