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