/* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain. Use, modify, and * redistribute this code in any way without acknowledgement. */ package java.util.concurrent; import java.security.AccessControlContext; import java.security.AccessController; import java.security.PrivilegedAction; import java.security.PrivilegedExceptionAction; import java.util.List; /** * Provides default implementation of {@link ExecutorService} * execution methods. This class implements the submit and * invoke methods using the default {@link FutureTask} and * {@link PrivilegedFutureTask} classes provided in this package. For * example, the the implementation of submit(Runnable) * creates an associated FutureTask that is executed and * returned. Subclasses overriding these methods to use different * {@link Future} implementations should do so consistently for each * of these methods. * * @since 1.5 * @author Doug Lea */ public abstract class AbstractExecutorService implements ExecutorService { public Future submit(Runnable task) { FutureTask ftask = new FutureTask(task, Boolean.TRUE); execute(ftask); return ftask; } public Future submit(Callable task) { FutureTask ftask = new FutureTask(task); execute(ftask); return ftask; } public void invoke(Runnable task) throws ExecutionException, InterruptedException { FutureTask ftask = new FutureTask(task, Boolean.TRUE); execute(ftask); ftask.get(); } public T invoke(Callable task) throws ExecutionException, InterruptedException { FutureTask ftask = new FutureTask(task); execute(ftask); return ftask.get(); } public Future submit(PrivilegedAction action) { Callable task = new PrivilegedActionAdapter(action); FutureTask future = new PrivilegedFutureTask(task); execute(future); return future; } public Future submit(PrivilegedExceptionAction action) { Callable task = new PrivilegedExceptionActionAdapter(action); FutureTask future = new PrivilegedFutureTask(task); execute(future); return future; } private static class PrivilegedActionAdapter implements Callable { PrivilegedActionAdapter(PrivilegedAction action) { this.action = action; } public Object call () { return action.run(); } private final PrivilegedAction action; } private static class PrivilegedExceptionActionAdapter implements Callable { PrivilegedExceptionActionAdapter(PrivilegedExceptionAction action) { this.action = action; } public Object call () throws Exception { return action.run(); } private final PrivilegedExceptionAction action; } }