An {@link ExecutorService} that executes each submitted task using one of possibly several pooled threads, normally configured using {@link Executors} factory methods.
public class ThreadPoolExecutor extends AbstractExecutorService public abstract class AbstractExecutorService implements ExecutorService public interface ExecutorService extends Executor { void shutdown(); // 关闭所有任务 List<Runnable> shutdownNow(); // 立即关闭所有任务 boolean isShutdown(); // 是否被关闭 boolean isTerminated(); // 是否被终止运行 Future<?> submit(Runnable task); // 提交任务到线程池中运行,最后还是交于execute方法执行 .... } public interface Executor { void execute(Runnable command); // 执行任务 }
ThreadPoolExecutor类构造函数参数分析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); } public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { .... }
/** * <dt>Hook methods</dt> * * <dd>This class provides {@code protected} overridable * {@link #beforeExecute(Thread, Runnable)} and * {@link #afterExecute(Runnable, Throwable)} methods that are called * before and after execution of each task. These can be used to * manipulate the execution environment; for example, reinitializing * ThreadLocals, gathering statistics, or adding log entries. */