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, |
14 |
> |
* <tt>invokeAny</tt> and <tt>invokeAll</tt> methods using a |
15 |
> |
* {@link RunnableFuture} returned by <tt>newTaskFor</tt>, which defaults |
16 |
> |
* to the {@link FutureTask} class provided in this package. For example, |
17 |
|
* the implementation of <tt>submit(Runnable)</tt> creates an |
18 |
< |
* associated <tt>FutureTask</tt> that is executed and |
19 |
< |
* returned. Subclasses overriding these methods to use different |
20 |
< |
* {@link Future} implementations should do so consistently for each |
21 |
< |
* of these methods. |
18 |
> |
* associated <tt>RunnableFuture</tt> that is executed and |
19 |
> |
* returned. Subclasses may override the <tt>newTaskFor</tt> methods |
20 |
> |
* to return other <tt>RunnableFuture</tt> implementations than |
21 |
> |
* <tt>FutureTask</tt>. |
22 |
|
* |
23 |
|
* @since 1.5 |
24 |
|
* @author Doug Lea |
25 |
|
*/ |
26 |
|
public abstract class AbstractExecutorService implements ExecutorService { |
27 |
|
|
28 |
+ |
/** |
29 |
+ |
* Returns a <tt>RunnableFuture</tt> for the given runnable and default |
30 |
+ |
* value. |
31 |
+ |
* @param runnable the runnable task being wrapped |
32 |
+ |
* @param value the default value for the returned future |
33 |
+ |
* @return a RunnableFuture which when run will run the underlying |
34 |
+ |
* runnable and which, as a Future, will yield the given value as its result |
35 |
+ |
* and provide for cancellation of the underlying task. |
36 |
+ |
*/ |
37 |
+ |
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { |
38 |
+ |
return new FutureTask<T>(runnable, value); |
39 |
+ |
} |
40 |
+ |
|
41 |
+ |
/** |
42 |
+ |
* Returns a <tt>RunnableFuture</tt> for the given callable task. |
43 |
+ |
* @param callable the callable task being wrapped |
44 |
+ |
* @return a RunnableFuture which when run will call the underlying |
45 |
+ |
* callable and which, as a Future, will yield the callable's result |
46 |
+ |
* as its result and provide for cancellation of the underlying task. |
47 |
+ |
*/ |
48 |
+ |
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { |
49 |
+ |
return new FutureTask<T>(callable); |
50 |
+ |
} |
51 |
+ |
|
52 |
|
public Future<?> submit(Runnable task) { |
53 |
|
if (task == null) throw new NullPointerException(); |
54 |
< |
FutureTask<Object> ftask = new FutureTask<Object>(task, null); |
54 |
> |
RunnableFuture<Object> ftask = newTaskFor(task, null); |
55 |
|
execute(ftask); |
56 |
|
return ftask; |
57 |
|
} |
58 |
|
|
59 |
|
public <T> Future<T> submit(Runnable task, T result) { |
60 |
|
if (task == null) throw new NullPointerException(); |
61 |
< |
FutureTask<T> ftask = new FutureTask<T>(task, result); |
61 |
> |
RunnableFuture<T> ftask = newTaskFor(task, result); |
62 |
|
execute(ftask); |
63 |
|
return ftask; |
64 |
|
} |
65 |
|
|
66 |
|
public <T> Future<T> submit(Callable<T> task) { |
67 |
|
if (task == null) throw new NullPointerException(); |
68 |
< |
FutureTask<T> ftask = new FutureTask<T>(task); |
68 |
> |
RunnableFuture<T> ftask = newTaskFor(task); |
69 |
|
execute(ftask); |
70 |
|
return ftask; |
71 |
|
} |
172 |
|
boolean done = false; |
173 |
|
try { |
174 |
|
for (Callable<T> t : tasks) { |
175 |
< |
FutureTask<T> f = new FutureTask<T>(t); |
175 |
> |
RunnableFuture<T> f = newTaskFor(t); |
176 |
|
futures.add(f); |
177 |
|
execute(f); |
178 |
|
} |
204 |
|
boolean done = false; |
205 |
|
try { |
206 |
|
for (Callable<T> t : tasks) |
207 |
< |
futures.add(new FutureTask<T>(t)); |
207 |
> |
futures.add(newTaskFor(t)); |
208 |
|
|
209 |
|
long lastTime = System.nanoTime(); |
210 |
|
|