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.List; |
10 |
import java.security.PrivilegedAction; |
11 |
import java.security.PrivilegedExceptionAction; |
12 |
|
13 |
/** |
14 |
* An <tt>Executor</tt> that provides methods to manage termination. |
15 |
* An <tt>ExecutorService</tt> can be shut down, which will cause it |
16 |
* to stop accepting new tasks. After being shut down, the executor |
17 |
* will eventually terminate, at which point no tasks are actively |
18 |
* executing, no tasks are awaiting execution, and no new tasks can be |
19 |
* submitted. |
20 |
* |
21 |
* <p>The {@link Executors} class provides factory methods for the |
22 |
* executor services provided in this package. |
23 |
* |
24 |
* @since 1.5 |
25 |
* @author Doug Lea |
26 |
*/ |
27 |
public interface ExecutorService extends Executor { |
28 |
|
29 |
/** |
30 |
* Submits a Runnable task for execution and returns a Future |
31 |
* representing that task. |
32 |
* |
33 |
* @param task the task to submit |
34 |
* @return a Future representing pending completion of the task, |
35 |
* and whose <tt>get()</tt> method will return an arbitrary value |
36 |
* upon completion |
37 |
* @throws RejectedExecutionException if task cannot be scheduled |
38 |
* for execution |
39 |
*/ |
40 |
Future<?> submit(Runnable task); |
41 |
|
42 |
/** |
43 |
* Submits a value-returning task for execution and returns a Future |
44 |
* representing the pending results of the task. |
45 |
* |
46 |
* @param task the task to submit |
47 |
* @return a Future representing pending completion of the task |
48 |
* @throws RejectedExecutionException if task cannot be scheduled |
49 |
* for execution |
50 |
*/ |
51 |
<T> Future<T> submit(Callable<T> task); |
52 |
|
53 |
/** |
54 |
* Executes a Runnable task and blocks until it completes normally |
55 |
* or throws an exception. |
56 |
* |
57 |
* @param task the task to submit |
58 |
* @throws RejectedExecutionException if task cannot be scheduled |
59 |
* for execution |
60 |
* @throws ExecutionException if the task encountered an exception |
61 |
* while executing |
62 |
*/ |
63 |
void invoke(Runnable task) throws ExecutionException, InterruptedException; |
64 |
|
65 |
/** |
66 |
* Executes a value-returning task and blocks until it returns a |
67 |
* value or throws an exception. |
68 |
* |
69 |
* @param task the task to submit |
70 |
* @return a Future representing pending completion of the task |
71 |
* @throws RejectedExecutionException if task cannot be scheduled |
72 |
* for execution |
73 |
* @throws InterruptedException if interrupted while waiting for |
74 |
* completion |
75 |
* @throws ExecutionException if the task encountered an exception |
76 |
* while executing |
77 |
*/ |
78 |
<T> T invoke(Callable<T> task) throws ExecutionException, InterruptedException; |
79 |
|
80 |
|
81 |
/** |
82 |
* Submits a privileged action for execution under the current |
83 |
* access control context and returns a Future representing the |
84 |
* pending result object of that action. |
85 |
* |
86 |
* @param action the action to submit |
87 |
* @return a Future representing pending completion of the action |
88 |
* @throws RejectedExecutionException if action cannot be scheduled |
89 |
* for execution |
90 |
*/ |
91 |
Future<Object> submit(PrivilegedAction action); |
92 |
|
93 |
/** |
94 |
* Submits a privileged exception action for execution under the current |
95 |
* access control context and returns a Future representing the pending |
96 |
* result object of that action. |
97 |
* |
98 |
* @param action the action to submit |
99 |
* @return a Future representing pending completion of the action |
100 |
* @throws RejectedExecutionException if action cannot be scheduled |
101 |
* for execution |
102 |
*/ |
103 |
Future<Object> submit(PrivilegedExceptionAction action); |
104 |
|
105 |
|
106 |
/** |
107 |
* Initiates an orderly shutdown in which previously submitted |
108 |
* tasks are executed, but no new tasks will be |
109 |
* accepted. Invocation has no additional effect if already shut |
110 |
* down. |
111 |
* |
112 |
*/ |
113 |
void shutdown(); |
114 |
|
115 |
/** |
116 |
* Attempts to stop all actively executing tasks, halts the |
117 |
* processing of waiting tasks, and returns a list of the tasks that were |
118 |
* awaiting execution. |
119 |
* |
120 |
* <p>There are no guarantees beyond best-effort attempts to stop |
121 |
* processing actively executing tasks. For example, typical |
122 |
* implementations will cancel via {@link Thread#interrupt}, so if any |
123 |
* tasks mask or fail to respond to interrupts, they may never terminate. |
124 |
* |
125 |
* @return list of tasks that never commenced execution |
126 |
*/ |
127 |
List shutdownNow(); |
128 |
|
129 |
/** |
130 |
* Returns <tt>true</tt> if this executor has been shut down. |
131 |
* |
132 |
* @return <tt>true</tt> if this executor has been shut down |
133 |
*/ |
134 |
boolean isShutdown(); |
135 |
|
136 |
/** |
137 |
* Returns <tt>true</tt> if all tasks have completed following shut down. |
138 |
* Note that <tt>isTerminated</tt> is never <tt>true</tt> unless |
139 |
* either <tt>shutdown</tt> or <tt>shutdownNow</tt> was called first. |
140 |
* |
141 |
* @return <tt>true</tt> if all tasks have completed following shut down |
142 |
*/ |
143 |
boolean isTerminated(); |
144 |
|
145 |
/** |
146 |
* Blocks until all tasks have completed execution after a shutdown |
147 |
* request, or the timeout occurs, or the current thread is |
148 |
* interrupted, whichever happens first. |
149 |
* |
150 |
* @param timeout the maximum time to wait |
151 |
* @param unit the time unit of the timeout argument |
152 |
* @return <tt>true</tt> if this executor terminated and <tt>false</tt> |
153 |
* if the timeout elapsed before termination |
154 |
* @throws InterruptedException if interrupted while waiting |
155 |
*/ |
156 |
boolean awaitTermination(long timeout, TimeUnit unit) |
157 |
throws InterruptedException; |
158 |
|
159 |
} |