image.png

  • 线程池一共是两大类,ForkJoinPoolThreadPoolExecutor
  • 通过Executors创建的线程池要么是ForkJoinPool,要么就是不同参数值的ThreadPoolExecutor

    Executor接口源码

    1. public interface Executor {
    2. void execute(Runnable command);
    3. }
  • 顶层接口

    ExecutorService接口源码

    ```java public interface ExecutorService extends Executor {

    /**

    • 不接受新的任务,已接收的任务执行完毕结束 */ void shutdown();

    /**

    • 立即中断现有执行的线程 */ List shutdownNow();

    /**

    • Returns {@code true} if this executor has been shut down. *
    • @return {@code true} if this executor has been shut down */ boolean isShutdown();

    /*

    • 要么shutdown调用,要么shutdownNow被调用,否则不会返回true */ boolean isTerminated();

    /**

    • Blocks until all tasks have completed execution after a shutdown
    • request, or the timeout occurs, or the current thread is
    • interrupted, whichever happens first.
    • 阻塞当前线程,知道下面三者之一发生
    • 1.在shutdown()后并且所有任务完成
    • 2.指定等待时间超时了
    • 3.当前线程被中断了 */ boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;

    /**

    • 实际底层还是调用execute(FutureTask) */ Future submit(Callable task);

    /**

    • */ Future submit(Runnable task, T result);

    /**

    • submit */ Future<?> submit(Runnable task);

    /**

    • */ List> invokeAll(Collection<? extends Callable> tasks) throws InterruptedException;

    /**

    • 所有任务结束后返回 */ List> invokeAll(Collection<? extends Callable> tasks,
      1. long timeout, TimeUnit unit)
      throws InterruptedException;

    /* */ T invokeAny(Collection<? extends Callable> tasks) throws InterruptedException, ExecutionException;

    /**

    • 任意一个任务接收就返回 */ T invokeAny(Collection<? extends Callable> tasks,
      1. long timeout, TimeUnit unit)
      throws InterruptedException, ExecutionException, TimeoutException; }
  1. <a name="A3IlK"></a>
  2. # AbstractExecutorService抽象类
  3. - 抽象实现
  4. <a name="dscMD"></a>
  5. ## newTaskFor()
  6. ```java
  7. /**
  8. * 均封装 FutureTask
  9. *
  10. */
  11. protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
  12. return new FutureTask<T>(runnable, value);
  13. }
  14. /**
  15. * Returns a {@code RunnableFuture} for the given callable task.
  16. *
  17. */
  18. protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
  19. return new FutureTask<T>(callable);
  20. }

submit()

  • submit方法实际也是调用了execute方法 ```java

    public Future<?> submit(Runnable task) { if (task == null) throw new NullPointerException(); RunnableFuture ftask = newTaskFor(task, null); *execute(ftask); return ftask; }

  1. public <T> Future<T> submit(Runnable task, T result) {
  2. if (task == null) throw new NullPointerException();
  3. RunnableFuture<T> ftask = newTaskFor(task, result);
  4. *execute(ftask);
  5. return ftask;
  6. }
  7. public <T> Future<T> submit(Callable<T> task) {
  8. if (task == null) throw new NullPointerException();
  9. RunnableFuture<T> ftask = newTaskFor(task);
  10. *execute(ftask);
  11. return ftask;
  12. }
  1. <a name="bqMyB"></a>
  2. # ThreadPoolExecutor类
  3. 具体参数含义之前写过。
  4. <a name="pcHfC"></a>
  5. # Executors各种线程池特点
  6. ```java
  7. @Test
  8. public void testAllPool(){
  9. // 0队列,0核心线程,MAX_VALUE临时线程,适用于处理时间短但任务多的
  10. Executors.newCachedThreadPool();
  11. // 无限队列,5核心 0临时 适用于长任务
  12. Executors.newFixedThreadPool(5);
  13. // 无限队列,1核心 0临时 是特殊的FixedThreadPool
  14. Executors.newSingleThreadExecutor();
  15. // 延时队列,2核心 MAX_VALUE临时线程,定时任务线程池
  16. Executors.newScheduledThreadPool(2);
  17. // 核心线程为1的定时任务线程池
  18. Executors.newSingleThreadScheduledExecutor();
  19. // ForkJoinPool
  20. Executors.newWorkStealingPool();
  21. }

线程池状态轮转

image.png

  • RUNING->SHUTDOWN:调用了shutdown()
  • RUNING ->STOP: 显式调用shutdownNow()
  • SHUTDOWN->STOP:显式调用shutdownNow()
  • SHUTDOWN->TIDYING: 无工作线程和队列为空时
  • STOP->TIDYING: 无工作线程时
  • TIDYING->TERMINATED: 调用terminated()方法时