< prev index next >

src/java.base/share/classes/java/util/concurrent/ExecutorService.java

Print this page

118  *           System.err.println("Pool did not terminate");
119  *     }
120  *   } catch (InterruptedException ex) {
121  *     // (Re-)Cancel if current thread also interrupted
122  *     pool.shutdownNow();
123  *     // Preserve interrupt status
124  *     Thread.currentThread().interrupt();
125  *   }
126  * }}</pre>
127  *
128  * <p>Memory consistency effects: Actions in a thread prior to the
129  * submission of a {@code Runnable} or {@code Callable} task to an
130  * {@code ExecutorService}
131  * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
132  * any actions taken by that task, which in turn <i>happen-before</i> the
133  * result is retrieved via {@code Future.get()}.
134  *
135  * @since 1.5
136  * @author Doug Lea
137  */
138 public interface ExecutorService extends Executor {
139 
140     /**
141      * Initiates an orderly shutdown in which previously submitted
142      * tasks are executed, but no new tasks will be accepted.
143      * Invocation has no additional effect if already shut down.
144      *
145      * <p>This method does not wait for previously submitted tasks to
146      * complete execution.  Use {@link #awaitTermination awaitTermination}
147      * to do that.
148      *
149      * @throws SecurityException if a security manager exists and
150      *         shutting down this ExecutorService may manipulate
151      *         threads that the caller is not permitted to modify
152      *         because it does not hold {@link
153      *         java.lang.RuntimePermission}{@code ("modifyThread")},
154      *         or the security manager's {@code checkAccess} method
155      *         denies access.
156      */
157     void shutdown();
158 

351      * The results of this method are undefined if the given
352      * collection is modified while this operation is in progress.
353      *
354      * @param tasks the collection of tasks
355      * @param timeout the maximum time to wait
356      * @param unit the time unit of the timeout argument
357      * @param <T> the type of the values returned from the tasks
358      * @return the result returned by one of the tasks
359      * @throws InterruptedException if interrupted while waiting
360      * @throws NullPointerException if tasks, or unit, or any element
361      *         task subject to execution is {@code null}
362      * @throws TimeoutException if the given timeout elapses before
363      *         any task successfully completes
364      * @throws ExecutionException if no task successfully completes
365      * @throws RejectedExecutionException if tasks cannot be scheduled
366      *         for execution
367      */
368     <T> T invokeAny(Collection<? extends Callable<T>> tasks,
369                     long timeout, TimeUnit unit)
370         throws InterruptedException, ExecutionException, TimeoutException;
371 }

















































118  *           System.err.println("Pool did not terminate");
119  *     }
120  *   } catch (InterruptedException ex) {
121  *     // (Re-)Cancel if current thread also interrupted
122  *     pool.shutdownNow();
123  *     // Preserve interrupt status
124  *     Thread.currentThread().interrupt();
125  *   }
126  * }}</pre>
127  *
128  * <p>Memory consistency effects: Actions in a thread prior to the
129  * submission of a {@code Runnable} or {@code Callable} task to an
130  * {@code ExecutorService}
131  * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
132  * any actions taken by that task, which in turn <i>happen-before</i> the
133  * result is retrieved via {@code Future.get()}.
134  *
135  * @since 1.5
136  * @author Doug Lea
137  */
138 public interface ExecutorService extends Executor, AutoCloseable {
139 
140     /**
141      * Initiates an orderly shutdown in which previously submitted
142      * tasks are executed, but no new tasks will be accepted.
143      * Invocation has no additional effect if already shut down.
144      *
145      * <p>This method does not wait for previously submitted tasks to
146      * complete execution.  Use {@link #awaitTermination awaitTermination}
147      * to do that.
148      *
149      * @throws SecurityException if a security manager exists and
150      *         shutting down this ExecutorService may manipulate
151      *         threads that the caller is not permitted to modify
152      *         because it does not hold {@link
153      *         java.lang.RuntimePermission}{@code ("modifyThread")},
154      *         or the security manager's {@code checkAccess} method
155      *         denies access.
156      */
157     void shutdown();
158 

351      * The results of this method are undefined if the given
352      * collection is modified while this operation is in progress.
353      *
354      * @param tasks the collection of tasks
355      * @param timeout the maximum time to wait
356      * @param unit the time unit of the timeout argument
357      * @param <T> the type of the values returned from the tasks
358      * @return the result returned by one of the tasks
359      * @throws InterruptedException if interrupted while waiting
360      * @throws NullPointerException if tasks, or unit, or any element
361      *         task subject to execution is {@code null}
362      * @throws TimeoutException if the given timeout elapses before
363      *         any task successfully completes
364      * @throws ExecutionException if no task successfully completes
365      * @throws RejectedExecutionException if tasks cannot be scheduled
366      *         for execution
367      */
368     <T> T invokeAny(Collection<? extends Callable<T>> tasks,
369                     long timeout, TimeUnit unit)
370         throws InterruptedException, ExecutionException, TimeoutException;
371 
372     /**
373      * Initiates an orderly shutdown in which previously submitted tasks are
374      * executed, but no new tasks will be accepted. This method waits until all
375      * tasks have completed execution and the executor has terminated.
376      *
377      * <p> If interrupted while waiting, this method stops all executing tasks as
378      * if by invoking {@link #shutdownNow()}. It then continues to wait until all
379      * actively executing tasks have completed. Tasks that were awaiting
380      * execution are not executed. The interrupt status will be re-asserted
381      * before this method returns.
382      *
383      * <p> If already terminated, invoking this method has no effect.
384      *
385      * @implSpec
386      * The default implementation invokes {@code shutdown()} and waits for tasks
387      * to complete execution with {@code awaitTermination}.
388      *
389      * @throws SecurityException if a security manager exists and
390      *         shutting down this ExecutorService may manipulate
391      *         threads that the caller is not permitted to modify
392      *         because it does not hold {@link
393      *         java.lang.RuntimePermission}{@code ("modifyThread")},
394      *         or the security manager's {@code checkAccess} method
395      *         denies access.
396      * @since 99
397      */
398     @Override
399     default void close() {
400         boolean terminated = isTerminated();
401         if (!terminated) {
402             shutdown();
403             boolean interrupted = false;
404             while (!terminated) {
405                 try {
406                     terminated = awaitTermination(1L, TimeUnit.DAYS);
407                 } catch (InterruptedException e) {
408                     if (!interrupted) {
409                         shutdownNow();
410                         interrupted = true;
411                     }
412                 }
413             }
414             if (interrupted) {
415                 Thread.currentThread().interrupt();
416             }
417         }
418     }
419 }
< prev index next >