ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ExecutorService.java
Revision: 1.19
Committed: Sat Dec 20 14:00:05 2003 UTC (20 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.18: +5 -16 lines
Log Message:
Replace PrivilegedFutureTask with Executors.privilegedCallable

File Contents

# Content
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.util.Collection;
11 import java.security.PrivilegedAction;
12 import java.security.PrivilegedExceptionAction;
13
14 /**
15 * An <tt>Executor</tt> that provides methods to manage termination
16 * and methods that can produce a {@link Future} for tracking
17 * progress of one or more asynchronous tasks.
18 * <p>
19 *
20 * An <tt>ExecutorService</tt> can be shut down, which will cause it
21 * to stop accepting new tasks. After being shut down, the executor
22 * will eventually terminate, at which point no tasks are actively
23 * executing, no tasks are awaiting execution, and no new tasks can be
24 * submitted.
25 *
26 * <p> Method <tt>submit</tt> extends base method <tt>execute</tt> by
27 * creating and returning a {@link Future} that can be used to cancel
28 * execution and/or wait for completion. Methods <tt>invokeAny</tt>
29 * and <tt>invokeAll</tt> perform the most commonly useful forms of
30 * bulk execution, executing a collection of tasks and then waiting
31 * for at least one, or all to complete. (Class {@link
32 * ExecutorCompletionService} can be used to write customizable
33 * versions of such methods.)
34 *
35 * <p>The {@link Executors} class provides factory methods for the
36 * executor services provided in this package.
37 *
38 * @since 1.5
39 * @author Doug Lea
40 */
41 public interface ExecutorService extends Executor {
42
43
44 /**
45 * Initiates an orderly shutdown in which previously submitted
46 * tasks are executed, but no new tasks will be
47 * accepted. Invocation has no additional effect if already shut
48 * down.
49 * @throws SecurityException if a security manager exists and
50 * shutting down this ExecutorService may manipulate threads that
51 * the caller is not permitted to modify because it does not hold
52 * {@link java.lang.RuntimePermission}<tt>("modifyThread")</tt>,
53 * or the security manager's <tt>checkAccess</tt> method denies access.
54 */
55 void shutdown();
56
57 /**
58 * Attempts to stop all actively executing tasks, halts the
59 * processing of waiting tasks, and returns a list of the tasks that were
60 * awaiting execution.
61 *
62 * <p>There are no guarantees beyond best-effort attempts to stop
63 * processing actively executing tasks. For example, typical
64 * implementations will cancel via {@link Thread#interrupt}, so if any
65 * tasks mask or fail to respond to interrupts, they may never terminate.
66 *
67 * @return list of tasks that never commenced execution
68 * @throws SecurityException if a security manager exists and
69 * shutting down this ExecutorService may manipulate threads that
70 * the caller is not permitted to modify because it does not hold
71 * {@link java.lang.RuntimePermission}<tt>("modifyThread")</tt>,
72 * or the security manager's <tt>checkAccess</tt> method denies access.
73 */
74 List<Runnable> shutdownNow();
75
76 /**
77 * Returns <tt>true</tt> if this executor has been shut down.
78 *
79 * @return <tt>true</tt> if this executor has been shut down
80 */
81 boolean isShutdown();
82
83 /**
84 * Returns <tt>true</tt> if all tasks have completed following shut down.
85 * Note that <tt>isTerminated</tt> is never <tt>true</tt> unless
86 * either <tt>shutdown</tt> or <tt>shutdownNow</tt> was called first.
87 *
88 * @return <tt>true</tt> if all tasks have completed following shut down
89 */
90 boolean isTerminated();
91
92 /**
93 * Blocks until all tasks have completed execution after a shutdown
94 * request, or the timeout occurs, or the current thread is
95 * interrupted, whichever happens first.
96 *
97 * @param timeout the maximum time to wait
98 * @param unit the time unit of the timeout argument
99 * @return <tt>true</tt> if this executor terminated and <tt>false</tt>
100 * if the timeout elapsed before termination
101 * @throws InterruptedException if interrupted while waiting
102 */
103 boolean awaitTermination(long timeout, TimeUnit unit)
104 throws InterruptedException;
105
106
107 /**
108 * Submits a value-returning task for execution and returns a Future
109 * representing the pending results of the task.
110 *
111 * <p>
112 * Note that if you would like to immediately block waiting
113 * for a task, you can use constructions of the form
114 * <tt>result = exec.submit(aCallable).get();</tt>
115 * @param task the task to submit
116 * @return a Future representing pending completion of the task
117 * @throws RejectedExecutionException if task cannot be scheduled
118 * for execution
119 */
120 <T> Future<T> submit(Callable<T> task);
121
122 /**
123 * Submits a Runnable task for execution and returns a Future
124 * representing that task.
125 *
126 * @param task the task to submit
127 * @return a Future representing pending completion of the task,
128 * and whose <tt>get()</tt> method will return <tt>null</tt>
129 * upon completion.
130 * @throws RejectedExecutionException if task cannot be scheduled
131 * for execution
132 */
133 Future<?> submit(Runnable task);
134
135 /**
136 * Executes the given tasks, returning their results
137 * when all complete.
138 * Note that a <em>completed</em> task could have
139 * terminated either normally or by throwing an exception.
140 * @param tasks the collection of tasks
141 * @return A list of Futures representing the tasks, in the same
142 * sequential order as produced by the iterator for the given task list, each of which has
143 * completed.
144 * @throws InterruptedException if interrupted while waiting, in
145 * which case unfinished tasks are cancelled.
146 * @throws NullPointerException if tasks or any of its elements are <tt>null</tt>
147 * @throws RejectedExecutionException if any task cannot be scheduled
148 * for execution
149 */
150
151 <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks)
152 throws InterruptedException;
153
154 /**
155 * Executes the given tasks, returning their results
156 * when all complete or the timeout expires, whichever happens first.
157 * Upon return, tasks that have not completed are cancelled.
158 * Note that a <em>completed</em> task could have
159 * terminated either normally or by throwing an exception.
160 * @param tasks the collection of tasks
161 * @param timeout the maximum time to wait
162 * @param unit the time unit of the timeout argument
163 * @return A list of Futures representing the tasks, in the same
164 * sequential order as as produced by the iterator for the given task list. If the operation did
165 * not time out, each task will have completed. If it did time
166 * out, some of thiese tasks will not have completed.
167 * @throws InterruptedException if interrupted while waiting, in
168 * which case unfinished tasks are cancelled.
169 * @throws NullPointerException if tasks, any of its elements, or
170 * unit are <tt>null</tt>
171 * @throws RejectedExecutionException if any task cannot be scheduled
172 * for execution
173 */
174 <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks,
175 long timeout, TimeUnit unit)
176 throws InterruptedException;
177
178 /**
179 * Executes the given tasks, returning the result
180 * of one that has completed successfully (i.e., without throwing
181 * an exception), if any do. Upon normal or exceptional return,
182 * tasks that have not completed are cancelled.
183 * @param tasks the collection of tasks
184 * @return The result returned by one of the tasks.
185 * @throws InterruptedException if interrupted while waiting
186 * @throws NullPointerException if tasks or any of its elements
187 * are <tt>null</tt>
188 * @throws IllegalArgumentException if tasks empty
189 * @throws ExecutionException if no task successfully completes
190 * @throws RejectedExecutionException if tasks cannot be scheduled
191 * for execution
192 */
193 <T> T invokeAny(Collection<Callable<T>> tasks)
194 throws InterruptedException, ExecutionException;
195
196 /**
197 * Executes the given tasks, returning the result
198 * of one that has completed successfully (i.e., without throwing
199 * an exception), if any do before the given timeout elapses.
200 * Upon normal or exceptional return, tasks that have not
201 * completed are cancelled.
202 * @param tasks the collection of tasks
203 * @param timeout the maximum time to wait
204 * @param unit the time unit of the timeout argument
205 * @return The result returned by one of the tasks.
206 * @throws InterruptedException if interrupted while waiting
207 * @throws NullPointerException if tasks, any of its elements, or
208 * unit are <tt>null</tt>
209 * @throws TimeoutException if the given timeout elapses before
210 * any task successfully completes
211 * @throws ExecutionException if no task successfully completes
212 * @throws RejectedExecutionException if tasks cannot be scheduled
213 * for execution
214 */
215 <T> T invokeAny(Collection<Callable<T>> tasks,
216 long timeout, TimeUnit unit)
217 throws InterruptedException, ExecutionException, TimeoutException;
218
219 }