0N/A/*
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * This file is available under and governed by the GNU General Public
0N/A * License version 2 only, as published by the Free Software Foundation.
0N/A * However, the following notice accompanied the original version of this
0N/A * file:
0N/A *
0N/A * Written by Doug Lea with assistance from members of JCP JSR-166
0N/A * Expert Group and released to the public domain, as explained at
3984N/A * http://creativecommons.org/publicdomain/zero/1.0/
0N/A */
0N/A
0N/Apackage java.util.concurrent;
0N/Aimport java.util.*;
0N/A
0N/A/**
0N/A * Provides default implementations of {@link ExecutorService}
0N/A * execution methods. This class implements the <tt>submit</tt>,
0N/A * <tt>invokeAny</tt> and <tt>invokeAll</tt> methods using a
0N/A * {@link RunnableFuture} returned by <tt>newTaskFor</tt>, which defaults
0N/A * to the {@link FutureTask} class provided in this package. For example,
0N/A * the implementation of <tt>submit(Runnable)</tt> creates an
0N/A * associated <tt>RunnableFuture</tt> that is executed and
0N/A * returned. Subclasses may override the <tt>newTaskFor</tt> methods
0N/A * to return <tt>RunnableFuture</tt> implementations other than
0N/A * <tt>FutureTask</tt>.
0N/A *
0N/A * <p> <b>Extension example</b>. Here is a sketch of a class
0N/A * that customizes {@link ThreadPoolExecutor} to use
0N/A * a <tt>CustomTask</tt> class instead of the default <tt>FutureTask</tt>:
3203N/A * <pre> {@code
0N/A * public class CustomThreadPoolExecutor extends ThreadPoolExecutor {
0N/A *
3203N/A * static class CustomTask<V> implements RunnableFuture<V> {...}
0N/A *
3203N/A * protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
3203N/A * return new CustomTask<V>(c);
0N/A * }
3203N/A * protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
3203N/A * return new CustomTask<V>(r, v);
0N/A * }
0N/A * // ... add constructors, etc.
3203N/A * }}</pre>
3203N/A *
0N/A * @since 1.5
0N/A * @author Doug Lea
0N/A */
0N/Apublic abstract class AbstractExecutorService implements ExecutorService {
0N/A
0N/A /**
0N/A * Returns a <tt>RunnableFuture</tt> for the given runnable and default
0N/A * value.
0N/A *
0N/A * @param runnable the runnable task being wrapped
0N/A * @param value the default value for the returned future
0N/A * @return a <tt>RunnableFuture</tt> which when run will run the
0N/A * underlying runnable and which, as a <tt>Future</tt>, will yield
0N/A * the given value as its result and provide for cancellation of
0N/A * the underlying task.
0N/A * @since 1.6
0N/A */
0N/A protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
0N/A return new FutureTask<T>(runnable, value);
0N/A }
0N/A
0N/A /**
0N/A * Returns a <tt>RunnableFuture</tt> for the given callable task.
0N/A *
0N/A * @param callable the callable task being wrapped
0N/A * @return a <tt>RunnableFuture</tt> which when run will call the
0N/A * underlying callable and which, as a <tt>Future</tt>, will yield
0N/A * the callable's result as its result and provide for
0N/A * cancellation of the underlying task.
0N/A * @since 1.6
0N/A */
0N/A protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
0N/A return new FutureTask<T>(callable);
0N/A }
0N/A
0N/A /**
0N/A * @throws RejectedExecutionException {@inheritDoc}
0N/A * @throws NullPointerException {@inheritDoc}
0N/A */
0N/A public Future<?> submit(Runnable task) {
0N/A if (task == null) throw new NullPointerException();
3203N/A RunnableFuture<Void> ftask = newTaskFor(task, null);
0N/A execute(ftask);
0N/A return ftask;
0N/A }
0N/A
0N/A /**
0N/A * @throws RejectedExecutionException {@inheritDoc}
0N/A * @throws NullPointerException {@inheritDoc}
0N/A */
0N/A public <T> Future<T> submit(Runnable task, T result) {
0N/A if (task == null) throw new NullPointerException();
0N/A RunnableFuture<T> ftask = newTaskFor(task, result);
0N/A execute(ftask);
0N/A return ftask;
0N/A }
0N/A
0N/A /**
0N/A * @throws RejectedExecutionException {@inheritDoc}
0N/A * @throws NullPointerException {@inheritDoc}
0N/A */
0N/A public <T> Future<T> submit(Callable<T> task) {
0N/A if (task == null) throw new NullPointerException();
0N/A RunnableFuture<T> ftask = newTaskFor(task);
0N/A execute(ftask);
0N/A return ftask;
0N/A }
0N/A
0N/A /**
0N/A * the main mechanics of invokeAny.
0N/A */
0N/A private <T> T doInvokeAny(Collection<? extends Callable<T>> tasks,
0N/A boolean timed, long nanos)
0N/A throws InterruptedException, ExecutionException, TimeoutException {
0N/A if (tasks == null)
0N/A throw new NullPointerException();
0N/A int ntasks = tasks.size();
0N/A if (ntasks == 0)
0N/A throw new IllegalArgumentException();
0N/A List<Future<T>> futures= new ArrayList<Future<T>>(ntasks);
0N/A ExecutorCompletionService<T> ecs =
0N/A new ExecutorCompletionService<T>(this);
0N/A
0N/A // For efficiency, especially in executors with limited
0N/A // parallelism, check to see if previously submitted tasks are
0N/A // done before submitting more of them. This interleaving
0N/A // plus the exception mechanics account for messiness of main
0N/A // loop.
0N/A
0N/A try {
0N/A // Record exceptions so that if we fail to obtain any
0N/A // result, we can throw the last exception we got.
0N/A ExecutionException ee = null;
3203N/A long lastTime = timed ? System.nanoTime() : 0;
0N/A Iterator<? extends Callable<T>> it = tasks.iterator();
0N/A
0N/A // Start one task for sure; the rest incrementally
0N/A futures.add(ecs.submit(it.next()));
0N/A --ntasks;
0N/A int active = 1;
0N/A
0N/A for (;;) {
0N/A Future<T> f = ecs.poll();
0N/A if (f == null) {
0N/A if (ntasks > 0) {
0N/A --ntasks;
0N/A futures.add(ecs.submit(it.next()));
0N/A ++active;
0N/A }
0N/A else if (active == 0)
0N/A break;
0N/A else if (timed) {
0N/A f = ecs.poll(nanos, TimeUnit.NANOSECONDS);
0N/A if (f == null)
0N/A throw new TimeoutException();
0N/A long now = System.nanoTime();
0N/A nanos -= now - lastTime;
0N/A lastTime = now;
0N/A }
0N/A else
0N/A f = ecs.take();
0N/A }
0N/A if (f != null) {
0N/A --active;
0N/A try {
0N/A return f.get();
0N/A } catch (ExecutionException eex) {
0N/A ee = eex;
0N/A } catch (RuntimeException rex) {
0N/A ee = new ExecutionException(rex);
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (ee == null)
0N/A ee = new ExecutionException();
0N/A throw ee;
0N/A
0N/A } finally {
0N/A for (Future<T> f : futures)
0N/A f.cancel(true);
0N/A }
0N/A }
0N/A
0N/A public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
0N/A throws InterruptedException, ExecutionException {
0N/A try {
0N/A return doInvokeAny(tasks, false, 0);
0N/A } catch (TimeoutException cannotHappen) {
0N/A assert false;
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
0N/A long timeout, TimeUnit unit)
0N/A throws InterruptedException, ExecutionException, TimeoutException {
0N/A return doInvokeAny(tasks, true, unit.toNanos(timeout));
0N/A }
0N/A
0N/A public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
0N/A throws InterruptedException {
0N/A if (tasks == null)
0N/A throw new NullPointerException();
0N/A List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
0N/A boolean done = false;
0N/A try {
0N/A for (Callable<T> t : tasks) {
0N/A RunnableFuture<T> f = newTaskFor(t);
0N/A futures.add(f);
0N/A execute(f);
0N/A }
0N/A for (Future<T> f : futures) {
0N/A if (!f.isDone()) {
0N/A try {
0N/A f.get();
0N/A } catch (CancellationException ignore) {
0N/A } catch (ExecutionException ignore) {
0N/A }
0N/A }
0N/A }
0N/A done = true;
0N/A return futures;
0N/A } finally {
0N/A if (!done)
0N/A for (Future<T> f : futures)
0N/A f.cancel(true);
0N/A }
0N/A }
0N/A
0N/A public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
0N/A long timeout, TimeUnit unit)
0N/A throws InterruptedException {
0N/A if (tasks == null || unit == null)
0N/A throw new NullPointerException();
0N/A long nanos = unit.toNanos(timeout);
0N/A List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
0N/A boolean done = false;
0N/A try {
0N/A for (Callable<T> t : tasks)
0N/A futures.add(newTaskFor(t));
0N/A
0N/A long lastTime = System.nanoTime();
0N/A
0N/A // Interleave time checks and calls to execute in case
0N/A // executor doesn't have any/much parallelism.
0N/A Iterator<Future<T>> it = futures.iterator();
0N/A while (it.hasNext()) {
0N/A execute((Runnable)(it.next()));
0N/A long now = System.nanoTime();
0N/A nanos -= now - lastTime;
0N/A lastTime = now;
0N/A if (nanos <= 0)
0N/A return futures;
0N/A }
0N/A
0N/A for (Future<T> f : futures) {
0N/A if (!f.isDone()) {
0N/A if (nanos <= 0)
0N/A return futures;
0N/A try {
0N/A f.get(nanos, TimeUnit.NANOSECONDS);
0N/A } catch (CancellationException ignore) {
0N/A } catch (ExecutionException ignore) {
0N/A } catch (TimeoutException toe) {
0N/A return futures;
0N/A }
0N/A long now = System.nanoTime();
0N/A nanos -= now - lastTime;
0N/A lastTime = now;
0N/A }
0N/A }
0N/A done = true;
0N/A return futures;
0N/A } finally {
0N/A if (!done)
0N/A for (Future<T> f : futures)
0N/A f.cancel(true);
0N/A }
0N/A }
0N/A
0N/A}