并发学习笔记——ThreadPoolExecutor 01

本文详细介绍了Java的ThreadPoolExecutor线程池的构造方法、线程池状态转换、工作线程的添加与执行,以及四种不同的拒绝策略。通过分析execute方法的执行流程,展示了线程池如何处理任务的提交、执行与拒绝。
摘要由CSDN通过智能技术生成

1 ThreadPoolExecutor

1.1 构造方法

   public ThreadPoolExecutor( int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
    // ... 略...
    }
  • corePoolSize:核心线程数量,核心线程会一直存活,即使没有任务要执行;
  • maximumPoolSize:线程池维护的最大线程数量;
  • keepAliveTime:线程池空闲线程数量超过核心线程数量时,多余线程的存活时间;

1.2 线程池状态

  • RUNNING
    可接收新任务,可执行阻塞队列里的任务;

  • SHUTDOWN

    • RUNNING状态 执行—>shutdown 不接收新任务,可执行阻塞队列里的任务,正在执行的任务正常执行
  • STOP

    • RUNNING状态 执行—>shutdownNow 不接收新任务,不可执行阻塞队列里的任务,中断正在执行的任务
  • TIDYING
    当所有的任务已终止,ctl记录的"任务数量"为0,线程池会变为TIDYING状态。当线程池变为TIDYING状态时,会执行钩子函数terminated()。terminated()在ThreadPoolExecutor类中是空的,若用户想在线程池变为TIDYING时,进行相应的处理;可以通过重载terminated()函数来实现,

    • SHUTDOWN状态 阻塞队列、线程池均没有要执行的任务
    • STOP状态 线程池没有要执行的任务
      在这里插入图片描述
  • TERMINATED
    TIDYING状态时,执行完terminated()之后,就会由 TIDYING -> TERMINATED

    // 高3位保存线程池的状态 低29位表示线程池任务数量
    private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
    private static final int COUNT_BITS = Integer.SIZE - 3;
    private static final int CAPACITY   = (1 << COUNT_BITS) - 1;

    // runState is stored in the high-order bits
    private static final int RUNNING    = -1 << COUNT_BITS;		111
    private static final int SHUTDOWN   =  0 << COUNT_BITS;		000
    private static final int STOP       =  1 << COUNT_BITS;		001
    private static final int TIDYING    =  2 << COUNT_BITS;		010
    private static final int TERMINATED =  3 << COUNT_BITS;		011

2 workers

  • Runnable firstTask
  • final Thread thread
    private final HashSet workers = new HashSet();
 private final class Worker
        extends AbstractQueuedSynchronizer
        implements Runnable
{
	final Thread thread;
	Runnable firstTask;
	volatile long completedTasks;
	
	public void run() {
            runWorker(this);
        }
}

2.1 addWorker(Runnable firstTask, boolean core)

在这里插入图片描述

private boolean addWorker(Runnable firstTask, boolean core) {
        retry:
        // 自旋
        for (;;) {
        // private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
            int c = ctl.get();
            // 计算 runState 线程池的状态
            int rs = runStateOf(c);

            // Check if queue empty only if necessary.
            // 如果状态至少是shutdown 并且下面三个有一个是false >= shutdown
            // 1 rs == SHUTDOWN            ---true---->   rs>shutdown = true 
            // 如果处于 stop、terminated、tidying  
            // 2 firstTask == null         ---true---->  rs == SHUTDOWN &&(firstTask != null) = true
            // 线程池已经shutdown了 还要添加新任务 被拒绝
            // 3 ! workQueue.isEmpty()       ---true---->  (workQueue.isEmpty()) = true
            // 创建一个没有任务的线程(firstTask ),从workQueue 获取任务,如果workQueue为空就没必要添加新的worker了
            // 或者 
            if (   rs >= SHUTDOWN &&
                ! (rs == SHUTDOWN &&
                   firstTask == null &&
                   ! workQueue.isEmpty()))
                return false;
            // 内层循环
            for (;;) {
                
                int wc = workerCountOf(c); // 当前 worker数量
                // 如果当前数量 >= 容量 
                // 如果添加核心线程 并且 >= 核心线程数
                // 如果添加非核心线程 并且 >= 线程池最大线程数量
                // 添加失败
                if (wc >= CAPACITY ||
                    wc >= (core ? corePoolSize : maximumPoolSize))
                    return false;
                // CAS增加c
                if (compareAndIncrementWorkerCount(c))
                    break retry;
                // CAS 失败 并且状态也变了 跳到外层循环
                c = ctl.get();  // Re-read ctl
                if (runStateOf(c) != rs)
                    continue retry;
                // else CAS failed due to workerCount change; retry inner loop
            }
        }
         // 到这说明 work+1 成功了
        boolean workerStarted = false;
        boolean workerAdded = false;
        Worker w = null;
        try {
        // 创建一个worker
            w = new Worker(firstTask);
            // 获取线程
            final Thread t = w.thread;
            if (t != null) {
            // 获取锁
                final ReentrantLock mainLock = this.mainLock;
                // 上锁 
                mainLock.lock();
                try {
                    // Recheck while holding lock.
                    // Back out on ThreadFactory failure or if
                    // shut down before lock acquired.
                    int rs = runStateOf(ctl.get());
                    
                    if (rs < SHUTDOWN ||
                        (rs == SHUTDOWN && firstTask == null)) {
                        // 如果线程启动了 抛出异常
                        if (t.isAlive()) // precheck that t is startable
                            throw new IllegalThreadStateException();
                        // HashSet<Worker> workers 
                        workers.add(w);
                        int s = workers.size();
                        if (s > largestPoolSize)
                            largestPoolSize = s;
                        workerAdded = true;
                    }
                } finally {
                // 释放锁
                    mainLock.unlock();
                }
                if (workerAdded) {
                // 启动线程
                    t.start();
                    workerStarted = true;
                }
            }
        } finally {
            if (! workerStarted)
                addWorkerFailed(w);
        }
        return workerStarted;
    }

在这里插入图片描述

2.2 runWorker调用场景

2.2.1 执行拒绝策略的时候,直接调用run

在这里插入图片描述

2.2.2 在addWorker方法结束处

在这里插入图片描述

为什么能调用到 Worker类的run方法
  • t.start ----> t.start0---->t.run
  • Thread类中 run, 调用的是target属性的run方法
    -下面去找target
    在这里插入图片描述
target 由工厂方法传入 newThread(this) this就是Worker对象实例

在这里插入图片描述

默认的实现

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

这里将 Worker实例 赋值给 Thread 的 target
private void init(ThreadGroup g, Runnable target, String name,
                      long stackSize, AccessControlContext acc,
                      boolean inheritThreadLocals) {
        // .......
        this.target = target;
        // .......
    }

2.3 runWorker

final void runWorker(Worker w) {
        // 当前线程
        Thread wt = Thread.currentThread();
        // work里要执行的任务
        Runnable task = w.firstTask;
        w.firstTask = null;
        w.unlock(); // allow interrupts
        boolean completedAbruptly = true;
        try {
            while (task != null || (task = getTask()) != null) {
                w.lock();
                // If pool is stopping, ensure thread is interrupted;
                // if not, ensure thread is not interrupted.  This
                // requires a recheck in second case to deal with
                // shutdownNow race while clearing interrupt
                
               //  private static boolean runStateAtLeast(int c, int s) {
               //  return c >= s;
               //}
               // stop/tidying,terminated 
               // 如果线程池的状态是>=sotp,需要确保线程被中断
               // 如果线程池的状态不是>=stop,需要确保线程没被中断
                if ((runStateAtLeast(ctl.get(), STOP) ||
                     (Thread.interrupted() && 
                      runStateAtLeast(ctl.get(), STOP))) &&
                    !wt.isInterrupted())
                    wt.interrupt();
                try {
                    beforeExecute(wt, task);
                    Throwable thrown = null;
                    try {
                    // 执行任务
                        task.run();
                    } catch (RuntimeException x) {
                        thrown = x; throw x;
                    } catch (Error x) {
                        thrown = x; throw x;
                    } catch (Throwable x) {
                        thrown = x; throw new Error(x);
                    } finally {
                        afterExecute(task, thrown);
                    }
                } finally {
                // 任务执行完 删除任务 释放锁
                    task = null;
                    w.completedTasks++;
                    w.unlock();
                }
            }
            completedAbruptly = false;
        } finally {
            processWorkerExit(w, completedAbruptly);
        }
    }

3 handler 拒绝策略

private volatile RejectedExecutionHandler handler;
private static final RejectedExecutionHandler defaultHandler = new AbortPolicy();

3.1 CallerRunsPolicy

  • 由提交任务的线程 执行这个任务
    public static class CallerRunsPolicy implements RejectedExecutionHandler {
      
        public CallerRunsPolicy() { }

        /**
         * Executes task r in the caller's thread, unless the executor
         * has been shut down, in which case the task is discarded.
         */
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            if (!e.isShutdown()) {
                r.run();
            }
        }
    }

3.2 AbortPolicy

  • 拒绝任务 并抛出异常 RejectedExecutionException
    在这里插入图片描述
    public static class AbortPolicy implements RejectedExecutionHandler {

        /**
         * Always throws RejectedExecutionException.
         *
         */
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            throw new RejectedExecutionException("Task " + r.toString() +
                                                 " rejected from " +
                                                 e.toString());
        }
    }

3.3 DiscardPolicy

  • 拒绝任务,但是不抛出异常
    在这里插入图片描述
  public static class DiscardPolicy implements RejectedExecutionHandler {

        /**
         * Does nothing, which has the effect of discarding task r.
         *
         * @param r the runnable task requested to be executed
         * @param e the executor attempting to execute this task
         */
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
        }
    }

3.4 DiscardOldestPolicy

  • 如果线程池正在运行,抛弃阻塞队列头部的任务,然后重新execute 提交执行新任务
 public static class DiscardOldestPolicy implements RejectedExecutionHandler {
 
        /**
         * Obtains and ignores the next task that the executor
         * would otherwise execute, if one is immediately available,
         * and then retries execution of task r, unless the executor
         * is shut down, in which case task r is instead discarded.
         */
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            if (!e.isShutdown()) {
                e.getQueue().poll();
                e.execute(r);
            }
        }
    }

4 execute

  • 如果 线程池线程数量 < corePoolSize,创建新线程(核心线程)执行任务,然后返回;
  • 如果 工作线程数量 >= corePoolSize,或者 addWorker 失败,重新获取 线程池状态
  • 如果线程池处于运行状态,BlockingQueue添加新的worker(workQueue.offer(command) = true)
    • 再次检测线程池状态,如果不是运行态,从BlockingQueue移除任务,然后执行拒绝策略
    • 如果线程池处于运行状态,并且工作线程为0,在线程池里增加一个线程
  • 线程池不是运行状态或者BlockQueue添加失败 执行拒绝策略
    public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        int c = ctl.get();
        // 1 如果线程数量小于核心线程数量 创建新的核心线程执行任务
        if (workerCountOf(c) < corePoolSize) {
        // 如果添加成功 直接返回
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        // 2 如果 线程数量大于等于核心线程数 或者 添加线程失败 
        // 2.1 
        // 先判断线程池状态  return c < SHUTDOWN; 
        // 然后往阻塞队列添加线程
        if (isRunning(c) && workQueue.offer(command)) {
        // 重新获取线程池状态,判断新加的线程是否可以被执行
            int recheck = ctl.get();
            // 如果线程池状态,不是运行状态,从队列移除这个任务 
            if (! isRunning(recheck) && remove(command))
            // 移除成功以后执行拒绝策略
                reject(command);
            // 如果线程池的工作线程数量是 0 添加新(非核心)线程
            else if (workerCountOf(recheck) == 0)
            // 为了防止线程池的任务队列里有任务而没有线程可用的这种情况发生
                addWorker(null, false);
        }
        // 2.2 添加非核心线程失败 拒绝任务
        else if (!addWorker(command, false))
            reject(command);
    }

remove

 public boolean remove(Runnable task) {
        boolean removed = workQueue.remove(task);
        tryTerminate(); // In case SHUTDOWN and now empty
        return removed;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值