ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/AbstractExecutorService.java
Revision: 1.14
Committed: Mon Dec 22 00:48:14 2003 UTC (20 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.13: +4 -0 lines
Log Message:
Streamline status settting

File Contents

# Content
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 import java.util.*;
10
11 /**
12 * Provides default implementation of {@link ExecutorService}
13 * 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 * returned. Subclasses overriding these methods to use different
19 * {@link Future} implementations should do so consistently for each
20 * of these methods.
21 *
22 * @since 1.5
23 * @author Doug Lea
24 */
25 public abstract class AbstractExecutorService implements ExecutorService {
26
27 public Future<?> submit(Runnable task) {
28 if (task == null) throw new NullPointerException();
29 FutureTask<Object> ftask = new FutureTask<Object>(task, null);
30 execute(ftask);
31 return ftask;
32 }
33
34 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 public <T> Future<T> submit(Callable<T> task) {
42 if (task == null) throw new NullPointerException();
43 FutureTask<T> ftask = new FutureTask<T>(task);
44 execute(ftask);
45 return ftask;
46 }
47
48 // any/all methods, each a little bit different than the other
49
50
51 public <T> T invokeAny(Collection<Callable<T>> tasks)
52 throws InterruptedException, ExecutionException {
53 if (tasks == null)
54 throw new NullPointerException();
55 int n = tasks.size();
56 if (n == 0)
57 throw new IllegalArgumentException();
58 List<Future<T>> futures= new ArrayList<Future<T>>(n);
59 ExecutorCompletionService<T> ecs =
60 new ExecutorCompletionService<T>(this);
61 try {
62 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(InterruptedException ie) {
71 throw ie;
72 } catch(ExecutionException eex) {
73 ee = eex;
74 } catch(RuntimeException rex) {
75 re = rex;
76 }
77 }
78 if (ee != null)
79 throw ee;
80 if (re != null)
81 throw new ExecutionException(re);
82 throw new ExecutionException();
83 } finally {
84 for (Future<T> f : futures)
85 f.cancel(true);
86 }
87 }
88
89 public <T> T invokeAny(Collection<Callable<T>> tasks,
90 long timeout, TimeUnit unit)
91 throws InterruptedException, ExecutionException, TimeoutException {
92 if (tasks == null || unit == null)
93 throw new NullPointerException();
94 long nanos = unit.toNanos(timeout);
95 int n = tasks.size();
96 if (n == 0)
97 throw new IllegalArgumentException();
98 List<Future<T>> futures= new ArrayList<Future<T>>(n);
99 ExecutorCompletionService<T> ecs =
100 new ExecutorCompletionService<T>(this);
101 try {
102 for (Callable<T> t : tasks)
103 futures.add(ecs.submit(t));
104 ExecutionException ee = null;
105 RuntimeException re = null;
106 long lastTime = System.nanoTime();
107 while (n-- > 0) {
108 Future<T> f = ecs.poll(nanos, TimeUnit.NANOSECONDS);
109 if (f == null)
110 throw new TimeoutException();
111 try {
112 return f.get();
113 } catch(InterruptedException ie) {
114 throw ie;
115 } catch(ExecutionException eex) {
116 ee = eex;
117 } catch(RuntimeException rex) {
118 re = rex;
119 }
120 long now = System.nanoTime();
121 nanos -= now - lastTime;
122 lastTime = now;
123 }
124 if (ee != null)
125 throw ee;
126 if (re != null)
127 throw new ExecutionException(re);
128 throw new ExecutionException();
129 } finally {
130 for (Future<T> f : futures)
131 f.cancel(true);
132 }
133 }
134
135 public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks)
136 throws InterruptedException {
137 if (tasks == null)
138 throw new NullPointerException();
139 List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
140 boolean done = false;
141 try {
142 for (Callable<T> t : tasks) {
143 FutureTask<T> f = new FutureTask<T>(t);
144 futures.add(f);
145 execute(f);
146 }
147 for (Future<T> f : futures) {
148 if (!f.isDone()) {
149 try {
150 f.get();
151 } catch(CancellationException ignore) {
152 } catch(ExecutionException ignore) {
153 }
154 }
155 }
156 done = true;
157 return futures;
158 } finally {
159 if (!done)
160 for (Future<T> f : futures)
161 f.cancel(true);
162 }
163 }
164
165 public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks,
166 long timeout, TimeUnit unit)
167 throws InterruptedException {
168 if (tasks == null || unit == null)
169 throw new NullPointerException();
170 long nanos = unit.toNanos(timeout);
171 List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
172 boolean done = false;
173 try {
174 for (Callable<T> t : tasks) {
175 FutureTask<T> f = new FutureTask<T>(t);
176 futures.add(f);
177 execute(f);
178 }
179 long lastTime = System.nanoTime();
180 for (Future<T> f : futures) {
181 if (!f.isDone()) {
182 if (nanos < 0)
183 return futures;
184 try {
185 f.get(nanos, TimeUnit.NANOSECONDS);
186 } catch(CancellationException ignore) {
187 } catch(ExecutionException ignore) {
188 } catch(TimeoutException toe) {
189 return futures;
190 }
191 long now = System.nanoTime();
192 nanos -= now - lastTime;
193 lastTime = now;
194 }
195 }
196 done = true;
197 return futures;
198 } finally {
199 if (!done)
200 for (Future<T> f : futures)
201 f.cancel(true);
202 }
203 }
204
205 }