Davids原理探究:ThreadPoolExecutor原理

文章目录

    • ThreadPoolExecutor原理
      • 线程池状态及转换条件图
      • 饱和策略(当队列满并且线程个数达到maximunPoolSize后采取的策略)
      • Executors线程池类型
      • 核心方法1:execute(Runnable command)
      • 核心方法2:addWorker(Runnable firstTask, boolean core)
      • 核心方法3:run()
      • 核心方法4:shutDown()和shutDownNow()
      • 核心方法5:awaitTermination(long timeout, TimeUnit unit)
      • 核心方法6:tryTerminate()
      • 示例:继承ThreadPoolExecutor重写部分方法
      • 总结

ThreadPoolExecutor原理

关注可以查看更多粉丝专享blog~

线程池状态及转换条件图

Davids原理探究:ThreadPoolExecutor原理_第1张图片

饱和策略(当队列满并且线程个数达到maximunPoolSize后采取的策略)

  1. AbortPolicy:抛出异常。
  2. CallerRunsPolicy:使用调用者所在的线程来运行任务。
  3. DiscardOldestPolicy:调用poll丢弃一个任务,执行当前任务。
  4. DiscardPolicy:默默丢弃,不抛出异常。

Executors线程池类型

keepAliveTime线程空闲keepAliveTime后则回收。

  1. newFixedThreadPool
// 核心线程和最大线程数一样,队列长度为Integer.MAX_VALUE,keepAliveTime=0
public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
}

// 使用自定义线程创建工厂
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>(),
                                  threadFactory);
}
  1. newSingleThreadExecutor
// 核心线程和最大线程数都为1,队列长度为Integer.MAX_VALUE,keepAliveTime=0
public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>()));
}

// 使用自定义线程创建工厂
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>(),
                                threadFactory));
}
  1. newCachedThreadPool
// 初始核心线程数为0,最大线程数为Integer.MAX_VALUE,并且为同步阻塞队列,keepAliveTime=60
// 特殊之处在于,加入同步队列的任务会马上执行,同步队列里面最多只有一个任务
public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>());
}

// 使用自定义线程创建工厂
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
  return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>(),
                                  threadFactory);
}
  1. newScheduledThreadPool

核心方法1:execute(Runnable command)

public void execute(Runnable command) {
	// 参数校验
    if (command == null)
        throw new NullPointerException();
    // 获取当前线程池状态和线程个数变量组合值(高3位为状态,低29位为线程数量)
    int c = ctl.get();
    // 如果当前线程个数小于核心线程数则增加核心线程运行任务
    if (workerCountOf(c) < corePoolSize) {
        if (addWorker(command, true))
            return;
        c = ctl.get();
    }
    // 走到这一步说明线程数大于核心线程数,如果线程池处于RUNNING状态,则添加到阻塞队列
    if (isRunning(c) && workQueue.offer(command)) {
        int recheck = ctl.get();
        // 再次判断状态,期间可能会有其他线程执行shutdown等操作改变状态,如果不是RUNNING则移除任务,并执行拒绝策略
        if (! isRunning(recheck) && remove(command))
            reject(command);
        // 否则如果当前线程为空,则添加一个线程
        else if (workerCountOf(recheck) == 0)
            addWorker(null, false);
    }
    // 如果队列满,则新增线程,新增失败则执行拒绝策略
    else if (!addWorker(command, false))
        reject(command);
}

核心方法2:addWorker(Runnable firstTask, boolean core)

private boolean addWorker(Runnable firstTask, boolean core) {
    retry:
    // 1.增加线程个数
    for (;;) {
        int c = ctl.get();
        int rs = runStateOf(c);

        // 只在必要时检查队列是否为空
        if (rs >= SHUTDOWN &&
            ! (rs == SHUTDOWN &&
               firstTask == null &&
               ! workQueue.isEmpty()))
            return false;

		// 循环CAS增加线程个数
        for (;;) {
        	// 获取当前工作线程个数
            int wc = workerCountOf(c);
            // 如果线程个数超了则return false
            if (wc >= CAPACITY ||
                wc >= (core ? corePoolSize : maximumPoolSize))
                return false;
            // 增加工作线程个数
            if (compareAndIncrementWorkerCount(c))
                break retry;
            // 增加失败,则查看线程池状态是否变化了,如果发生变化则跳到外层循环重新尝试获取线程池状态,否则内层循环重新CAS增加线程个数
            c = ctl.get();
            if (runStateOf(c) != rs)
                continue retry;
        }
    }

	// 2.添加到工作队列
    boolean workerStarted = false;
    boolean workerAdded = false;
    Worker w = null;
    try {
    	// 构造worker,状态设置为-1禁止中断,直到runWorker时修改为可中断,刚构造的worker中断无意义
    	// Worker(Runnable firstTask) {
        //     setState(-1);
        //     this.firstTask = firstTask;
        //     this.thread = getThreadFactory().newThread(this);
        // }
        w = new Worker(firstTask);
        final Thread t = w.thread;
        if (t != null) {
        	// 获取独占锁,为了实现同步workers同步,存在多个线程调用了线程池的execute
            final ReentrantLock mainLock = this.mainLock;
            mainLock.lock();
            try {
            	// 重新获取线程池状态
                int rs = runStateOf(ctl.get());
                // 如果状态为可执行任务状态,或者为shutdown
                if (rs < SHUTDOWN ||
                    (rs == SHUTDOWN && firstTask == null)) {
                    // 检查线程是否可以启动,启动过则报错
                    // 方法isAlive() 的功能是判断当前的线程是否处于活动状态;活动状态就是线程已经启动尚未终止,那么这时候线程就是存活的,则返回true,否则则返回false;
                    if (t.isAlive())
                        throw new IllegalThreadStateException();
                    workers.add(w);
                    int s = workers.size();
                    if (s > largestPoolSize)
                        largestPoolSize = s;
                    workerAdded = true;
                }
            } finally {
            	// 释放锁
                mainLock.unlock();
            }
            // 如果添加成功则启动线程
            if (workerAdded) {
                t.start();
                workerStarted = true;
            }
        }
    } finally {
    	// 添加失败则重新获取锁,然后从移除当前worker,CAS workerCount - 1,并tryTerminate
        if (! workerStarted)
            addWorkerFailed(w);
    }
    return workerStarted;
}

// 执行中断的方法
private void interruptWorkers() {
   final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        for (Worker w : workers)
            w.interruptIfStarted();
    } finally {
        mainLock.unlock();
    }
}

void interruptIfStarted() {
  Thread t;
  	// 只有state >= 0 的线程才可以执行中断操作,所以构造的worker state = -1
    if (getState() >= 0 && (t = thread) != null && !t.isInterrupted()) {
        try {
            t.interrupt();
        } catch (SecurityException ignore) {
        }
    }
}

核心方法3:run()

// 执行任务内部调用runWorker(this)
public void run() {
    runWorker(this);
}

// 执行runWorker(Worker w)
final void runWorker(Worker w) {
    Thread wt = Thread.currentThread();
    Runnable task = w.firstTask;
    w.firstTask = null;
    // 
    w.unlock();
    // 控制processWorkerExit是否处理workerCount,默认true
    boolean completedAbruptly = true;
    try {
        while (task != null || (task = getTask()) != null) {
            w.lock();
            // 如果池正在停止,确保线程被中断;
            // 如果没有,确保线程不被中断。  
            // 这需要在第二种情况下重新检查以处理shutdownNow,同时清除中断
            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);
    }
}

// worker退出处理
private void processWorkerExit(Worker w, boolean completedAbruptly) {
	// 如果是异常中断则不调整workerCount
    if (completedAbruptly)
        decrementWorkerCount();

	// 获取独占锁
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
    	// 设置线程池完成任务数 += 当前worker完成任务数
        completedTaskCount += w.completedTasks;
        // 从worker队列中移除当前worker
        workers.remove(w);
    } finally {
        mainLock.unlock();
    }

	// 尝试设置线程池状态为TERMINATED
    tryTerminate();

    int c = ctl.get();
    if (runStateLessThan(c, STOP)) {
        if (!completedAbruptly) {
            int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
            if (min == 0 && ! workQueue.isEmpty())
                min = 1;
            if (workerCountOf(c) >= min)
                return; // replacement not needed
        }
        addWorker(null, false);
    }
}

核心方法4:shutDown()和shutDownNow()

public void shutdown() {
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
		// 检查当前线程是否有shutDown权限
        checkShutdownAccess();
		// 设置线程池状态,如果已经是该状态直接返回
        advanceRunState(SHUTDOWN);
		// 如果工作线程没有中断,并且没有正在运行则设置中断标志
        interruptIdleWorkers();
        onShutdown(); // hook for ScheduledThreadPoolExecutor
    } finally {
        mainLock.unlock();
    }
	// 尝试设置线程池状态为TERMINATED
    tryTerminate();
}

public List<Runnable> shutdownNow() {
    List<Runnable> tasks;
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        // 检查当前线程是否有shutDown权限
        checkShutdownAccess();
		// 设置线程池状态,如果已经是该状态直接返回
        advanceRunState(STOP);
		// 中断所有工作线程
        interruptWorkers();
		// 将队列中的元素移动到tasks列表
        tasks = drainQueue();
    } finally {
        mainLock.unlock();
    }
	// 尝试设置线程池状态为TERMINATED
    tryTerminate();
	// 返回队列中被丢弃的任务列表
    return tasks;
}

核心方法5:awaitTermination(long timeout, TimeUnit unit)

该方法调用会被阻塞,以下几种情况任意一个发生了就会导致该方法的执行:

  1. 所有任务执行完毕并且shutdown请求被调用
  2. 参数中定义的timeout时间到达
  3. 当前线程被打断

核心方法6:tryTerminate()

/**
 * 1.线程池处于RUNNING状态
 * 2.线程池已经处于TERMINATE
 * 3.线程池为SHUTDOWN状态并且队列不为空
 * 以上三种情况直接return
 */
final void tryTerminate() {
    for (;;) {
        int c = ctl.get();
        if (isRunning(c) ||
            runStateAtLeast(c, TIDYING) ||
            (runStateOf(c) == SHUTDOWN && ! workQueue.isEmpty()))
            return;
        // 有资格终止
        if (workerCountOf(c) != 0) {
            interruptIdleWorkers(ONLY_ONE);
            return;
        }
		// 获取锁尝试终止
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
        	// CAS修改线程池状态为TIDYING
            if (ctl.compareAndSet(c, ctlOf(TIDYING, 0))) {
                try {
                	// 成功则执行terminated方法(见示例:可继承重写)
                    terminated();
                } finally {
                	// 设置线程池状态为TERMINATED
                    ctl.set(ctlOf(TERMINATED, 0));
                    // 唤醒termination条件队列中的线程(核心方法5 awaitTermination)
                    termination.signalAll();
                }
                return;
            }
        } finally {
            mainLock.unlock();
        }
        // else retry on failed CAS
    }
}

示例:继承ThreadPoolExecutor重写部分方法

public class ThreadPoolExecutorTest extends ThreadPoolExecutor{

    public ThreadPoolExecutorTest( int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue ) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    @Override
    protected void terminated() {
        System.out.println("===它终止啦它终止啦!===");
        super.terminated();
    }

    @Override
    public void execute( Runnable command ) {
        System.out.println("有人execute我");
        super.execute(command);
    }

    @Override
    protected void beforeExecute( Thread t, Runnable r ) {
        System.out.println("===它快来啦它快来啦!===");
        super.beforeExecute(t, r);
    }

    @Override
    protected void afterExecute( Runnable r, Throwable t ) {
        System.out.println("===它走啦它走啦!===");
        super.afterExecute(r, t);
    }

    @SneakyThrows
    public static void main( String[] args ) {
        ThreadPoolExecutorTest threadPool = new ThreadPoolExecutorTest(1, 1, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
        threadPool.execute(new Thread(() -> System.out.println("干活!!!")));
        threadPool.awaitTermination(5, TimeUnit.SECONDS);
        System.out.println("wait 超时");
        threadPool.shutdownNow();
        threadPool.execute(new Thread(() -> System.out.println("我又来干活儿啦!")));
    }

}

// output shutdown之后的线程不能再次execute激活
有人execute我
===它快来啦它快来啦!===
干活!!!
===它走啦它走啦!===
wait 超时
===它终止啦它终止啦!===
有人execute我
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task Thread[Thread-1,5,main] rejected from *.*.*.juc.ThreadPoolExecutorTest@6193b845[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1]
	at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
	at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
	at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
	at *.*.*.juc.ThreadPoolExecutorTest.execute(ThreadPoolExecutorTest.java:32)
	at *.*.*.juc.ThreadPoolExecutorTest.main(ThreadPoolExecutorTest.java:55)

总结

线程池巧妙地使用了AtomicInteger来记录线程池的状态(高3位)和线程池中的线程个数(低29位)。通过线程池状态来控制任务的执行,每个Worker线程可以处理多个任务。线程池通过线程的复用减少了线程创建和销毁的开销。

你可能感兴趣的:(并发,Java,多线程)