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 |
|
|
import java.security.AccessControlContext; |
10 |
|
|
import java.security.AccessController; |
11 |
|
|
import java.security.PrivilegedAction; |
12 |
|
|
import java.security.PrivilegedExceptionAction; |
13 |
|
|
import java.util.List; |
14 |
|
|
|
15 |
|
|
/** |
16 |
dl |
1.2 |
* Provides default implementation of {@link ExecutorService} |
17 |
|
|
* execution methods. This class implements the <tt>submit</tt> and |
18 |
|
|
* <tt>invoke</tt> methods using the default {@link FutureTask} and |
19 |
|
|
* {@link PrivilegedFutureTask} classes provided in this package. For |
20 |
|
|
* example, the the implementation of <tt>submit(Runnable)</tt> |
21 |
|
|
* creates an associated <tt>FutureTask</tt> that is executed and |
22 |
|
|
* returned. Subclasses overriding these methods to use different |
23 |
|
|
* {@link Future} implementations should do so consistently for each |
24 |
|
|
* of these methods. |
25 |
tim |
1.1 |
* |
26 |
|
|
* @since 1.5 |
27 |
|
|
* @author Doug Lea |
28 |
|
|
*/ |
29 |
|
|
public abstract class AbstractExecutorService implements ExecutorService { |
30 |
|
|
|
31 |
|
|
public Future<?> submit(Runnable task) { |
32 |
|
|
FutureTask<?> ftask = new FutureTask<Boolean>(task, Boolean.TRUE); |
33 |
|
|
execute(ftask); |
34 |
|
|
return ftask; |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
public <T> Future<T> submit(Callable<T> task) { |
38 |
|
|
FutureTask<T> ftask = new FutureTask<T>(task); |
39 |
|
|
execute(ftask); |
40 |
|
|
return ftask; |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
public void invoke(Runnable task) throws ExecutionException, InterruptedException { |
44 |
|
|
FutureTask<?> ftask = new FutureTask<Boolean>(task, Boolean.TRUE); |
45 |
|
|
execute(ftask); |
46 |
|
|
ftask.get(); |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
public <T> T invoke(Callable<T> task) throws ExecutionException, InterruptedException { |
50 |
|
|
FutureTask<T> ftask = new FutureTask<T>(task); |
51 |
|
|
execute(ftask); |
52 |
|
|
return ftask.get(); |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
public Future<Object> submit(PrivilegedAction action) { |
56 |
|
|
Callable<Object> task = new PrivilegedActionAdapter(action); |
57 |
|
|
FutureTask<Object> future = new PrivilegedFutureTask<Object>(task); |
58 |
|
|
execute(future); |
59 |
|
|
return future; |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
public Future<Object> submit(PrivilegedExceptionAction action) { |
63 |
|
|
Callable<Object> task = new PrivilegedExceptionActionAdapter(action); |
64 |
|
|
FutureTask<Object> future = new PrivilegedFutureTask<Object>(task); |
65 |
|
|
execute(future); |
66 |
|
|
return future; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
private static class PrivilegedActionAdapter implements Callable<Object> { |
70 |
|
|
PrivilegedActionAdapter(PrivilegedAction action) { |
71 |
|
|
this.action = action; |
72 |
|
|
} |
73 |
|
|
public Object call () { |
74 |
|
|
return action.run(); |
75 |
|
|
} |
76 |
|
|
private final PrivilegedAction action; |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
private static class PrivilegedExceptionActionAdapter implements Callable<Object> { |
80 |
|
|
PrivilegedExceptionActionAdapter(PrivilegedExceptionAction action) { |
81 |
|
|
this.action = action; |
82 |
|
|
} |
83 |
|
|
public Object call () throws Exception { |
84 |
|
|
return action.run(); |
85 |
|
|
} |
86 |
|
|
private final PrivilegedExceptionAction action; |
87 |
|
|
} |
88 |
|
|
} |