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 |
|
|
* A partial <tt>ExecutorService</tt> implementation. |
17 |
|
|
* |
18 |
|
|
* @since 1.5 |
19 |
|
|
* @author Doug Lea |
20 |
|
|
*/ |
21 |
|
|
public abstract class AbstractExecutorService implements ExecutorService { |
22 |
|
|
|
23 |
|
|
public Future<?> submit(Runnable task) { |
24 |
|
|
FutureTask<?> ftask = new FutureTask<Boolean>(task, Boolean.TRUE); |
25 |
|
|
execute(ftask); |
26 |
|
|
return ftask; |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
public <T> Future<T> submit(Callable<T> task) { |
30 |
|
|
FutureTask<T> ftask = new FutureTask<T>(task); |
31 |
|
|
execute(ftask); |
32 |
|
|
return ftask; |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
public void invoke(Runnable task) throws ExecutionException, InterruptedException { |
36 |
|
|
FutureTask<?> ftask = new FutureTask<Boolean>(task, Boolean.TRUE); |
37 |
|
|
execute(ftask); |
38 |
|
|
ftask.get(); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
public <T> T invoke(Callable<T> task) throws ExecutionException, InterruptedException { |
42 |
|
|
FutureTask<T> ftask = new FutureTask<T>(task); |
43 |
|
|
execute(ftask); |
44 |
|
|
return ftask.get(); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
public Future<Object> submit(PrivilegedAction action) { |
48 |
|
|
Callable<Object> task = new PrivilegedActionAdapter(action); |
49 |
|
|
FutureTask<Object> future = new PrivilegedFutureTask<Object>(task); |
50 |
|
|
execute(future); |
51 |
|
|
return future; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
public Future<Object> submit(PrivilegedExceptionAction action) { |
55 |
|
|
Callable<Object> task = new PrivilegedExceptionActionAdapter(action); |
56 |
|
|
FutureTask<Object> future = new PrivilegedFutureTask<Object>(task); |
57 |
|
|
execute(future); |
58 |
|
|
return future; |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
private static class PrivilegedActionAdapter implements Callable<Object> { |
62 |
|
|
PrivilegedActionAdapter(PrivilegedAction action) { |
63 |
|
|
this.action = action; |
64 |
|
|
} |
65 |
|
|
public Object call () { |
66 |
|
|
return action.run(); |
67 |
|
|
} |
68 |
|
|
private final PrivilegedAction action; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
private static class PrivilegedExceptionActionAdapter implements Callable<Object> { |
72 |
|
|
PrivilegedExceptionActionAdapter(PrivilegedExceptionAction action) { |
73 |
|
|
this.action = action; |
74 |
|
|
} |
75 |
|
|
public Object call () throws Exception { |
76 |
|
|
return action.run(); |
77 |
|
|
} |
78 |
|
|
private final PrivilegedExceptionAction action; |
79 |
|
|
} |
80 |
|
|
} |