java线程池ThreadPoolExector源码分析

      java线程池ThreadPoolExector源码分析


 

今天研究了下ThreadPoolExector源码,大致上总结了以下几点跟大家分享下:

一、ThreadPoolExector几个主要变量

先了解下ThreadPoolExector中比较重要的几个变量。

             corePoolSize:核心线程数量

       maximumPoolSize:最大线程数量

 allowCoreThreadTimeOut:是否允许线程超时(设置为true时与keepAliveTime,TimeUnit一起起作用)

                keepAliveTime:线程存活时间(当线程池允许线程超时且运行中的线程数量超过corePoolSize时,会按照此变量设置时间关闭线程)

                        TimeUnit:单位(一般与keepAliveTime同时使用,供线程池判断是否满足关闭线程的条件)

          workQueue:缓冲队列

RejectedExecutionHandler:拒绝处理任务类(默认:AbortPolicy 会抛异常,见下方实例)。

                  threadFactory:线程工厂(默认:DefaultThreadFactory)

 

二、线程池的几个主要方法分析

  1 主方法:ThreadPoolExector的execute  

public void execute(Runnable command) {
    if (command ==null)
            thrownew NullPointerException();
            intc = ctl.get();

      //1 当前运行的线程数量小于核心线程数量,直接将任务加入worker启动运行。
        if (workerCountOf(c) <corePoolSize) {
            if (addWorker(command,true))
                return;
            c =ctl.get();
        } 
        
//2 运行线程数量大于核心线程数量时,上面的if分支针对大于corePoolSize,并且缓存队列加入任务操作成功的情况。 运行中并且将任务加入缓冲队列成功,正常来说这样已经完成了处理逻辑。 但是为了保险起见,增加了状态出现异常的确认判断,如果状态出现异常会继续remove操作,如果执行true,则按照拒绝处理策略驳回任务; if (isRunning(c) &&workQueue.offer(command)) { intrecheck = ctl.get(); if (!isRunning(recheck) && remove(command)) reject(command); elseif (workerCountOf(recheck) == 0) addWorker(null,false); } //3 这里针对运行线程数量超过了corePoolSize,并且缓存队列也已经放满的情况。 注意第二个参数是false,可以在下面addWorker方法看到,就是针对线程池最大线程数量maximumPoolSize的判断。 elseif (!addWorker(command,false)) reject(command); }

  2 关键方法:ThreadPoolExector的addWorker(增加工作线程)

private boolean addWorker(Runnable firstTask, boolean core) {
        retry:
        for (;;) {
            int c = ctl.get();
            int rs = runStateOf(c);

            // Check if queue empty only if necessary.
            if (rs >= SHUTDOWN &&
                ! (rs == SHUTDOWN &&
                   firstTask == null &&
                   ! workQueue.isEmpty()))
                return false;

            for (;;) {
                int wc = workerCountOf(c);
                if (wc >= CAPACITY ||
                    wc >= (core ? corePoolSize : maximumPoolSize))
                    return false;
                if (compareAndIncrementWorkerCount(c))
                    break retry;
                c = ctl.get();  // Re-read ctl
                if (runStateOf(c) != rs)
                    continue retry;
                // else CAS failed due to workerCount change; retry inner loop
            }
        }

        boolean workerStarted = false;
        boolean workerAdded = false;
        Worker w = null;
        try {
            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();
                        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;
    }

3  Worker中的runWorker方法,也是worker中的run方法主体。

public void run() {
            runWorker(this);
        }
final void runWorker(Worker w) {
        Thread wt = Thread.currentThread();
        Runnable task = w.firstTask;
        w.firstTask = null;
        w.unlock(); // allow interrupts
        boolean completedAbruptly = true;
        try {
       //执行任务,并且当执行完后再去获取新的task继续执行,getTask方法是由ThreadPoolExecutor提供,继续向下看。  
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 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); } }
private Runnable getTask() {
        boolean timedOut = false; // Did the last poll() time out?

        for (;;) {
            int c = ctl.get();
            int rs = runStateOf(c);

            // Check if queue empty only if necessary.
            if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
                decrementWorkerCount();
                return null;
            }

            int wc = workerCountOf(c);

            // Are workers subject to culling?
            boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;

       //如果运行线程数超过了最大线程数,但是缓存队列已经空了,这时递减worker数量。
       //如果有设置允许线程超时或者线程数量超过了核心线程数量,并且线程在规定时间内均未poll到任务且队列为空则递减worker数量
if ((wc > maximumPoolSize || (timed && timedOut)) && (wc > 1 || workQueue.isEmpty())) { if (compareAndDecrementWorkerCount(c)) return null; continue; } try { Runnable r = timed ? workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) : workQueue.take(); if (r != null) return r; timedOut = true; } catch (InterruptedException retry) { timedOut = false; } } }

三、测试实例:

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest {
    // 核心线程数量
    private static int corePoolSize = 3;
    // 最大线程数量
    private static int maxPoolSize = 5;
    // 线程存活时间:当线程数量超过corePoolSize时,10秒钟空闲即关闭线程
    private static int keepAliveTime = 10000;
    // 缓冲队列
    private static BlockingQueue<Runnable> workQueue = null;
    // 线程池
    private static ThreadPoolExecutor threadPoolExecutor = null;

    static {
        workQueue = new LinkedBlockingQueue<Runnable>(5);
        threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS,
                workQueue);
    }

    public static void main(String[] args) throws InterruptedException {
        try {
            for (int i = 0; i < 200; i++) {
                System.out.println("=========第" + i + "次");
                threadPoolExecutor.execute(new MyTask());
                System.out.println("线程池中正在执行的线程数量:" + threadPoolExecutor.getPoolSize());
                System.out.println("线程池缓存的任务队列数量:" + threadPoolExecutor.getQueue().size());
            }
        } finally {
            threadPoolExecutor.shutdown();
        }
    }
}

 

运行结果:

 

 

=========第0次

线程池中正在执行的线程数量:1

线程池缓存的任务队列数量:0

=========第1次

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:2

线程池缓存的任务队列数量:0

正在执行任务:pool-1-thread-2

=========第2次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第3次

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3         由于corePoolSize已经满了,所以开始放缓冲队列

线程池缓存的任务队列数量:1

=========第4次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:2

=========第5次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:3

=========第6次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:4

=========第7次

执行完毕:pool-1-thread-2

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-2

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:3

=========第8次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:4

=========第9次

执行完毕:pool-1-thread-2

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:4

=========第10次

执行完毕:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:4

=========第11次

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-1

执行完毕:pool-1-thread-2

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:3

正在执行任务:pool-1-thread-2

执行完毕:pool-1-thread-1

执行完毕:pool-1-thread-3

=========第12次

正在执行任务:pool-1-thread-3

执行完毕:pool-1-thread-2

正在执行任务:pool-1-thread-1

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第13次

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-3

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

执行完毕:pool-1-thread-3

执行完毕:pool-1-thread-2

线程池缓存的任务队列数量:0

=========第14次

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-1

执行完毕:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第15次

线程池中正在执行的线程数量:3

正在执行任务:pool-1-thread-2

线程池缓存的任务队列数量:0

=========第16次

执行完毕:pool-1-thread-2

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第17次

执行完毕:pool-1-thread-1

线程池中正在执行的线程数量:3

正在执行任务:pool-1-thread-3

线程池缓存的任务队列数量:0

=========第18次

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

执行完毕:pool-1-thread-2

=========第19次

线程池中正在执行的线程数量:3

正在执行任务:pool-1-thread-1

线程池缓存的任务队列数量:0

=========第20次

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第21次

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第22次

执行完毕:pool-1-thread-2

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第23次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第24次

正在执行任务:pool-1-thread-3

正在执行任务:pool-1-thread-2

执行完毕:pool-1-thread-1

执行完毕:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

执行完毕:pool-1-thread-2

=========第25次

线程池中正在执行的线程数量:3

正在执行任务:pool-1-thread-1

线程池缓存的任务队列数量:0

=========第26次

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第27次

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第28次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:1

=========第29次

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:1

=========第30次

线程池中正在执行的线程数量:3

执行完毕:pool-1-thread-2

线程池缓存的任务队列数量:1

=========第31次

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-3

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第32次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:1

=========第33次

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-2

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

执行完毕:pool-1-thread-3

执行完毕:pool-1-thread-2

执行完毕:pool-1-thread-1

线程池缓存的任务队列数量:1

=========第34次

正在执行任务:pool-1-thread-3

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第35次

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

执行完毕:pool-1-thread-1

执行完毕:pool-1-thread-2

=========第36次

线程池中正在执行的线程数量:3

正在执行任务:pool-1-thread-3

线程池缓存的任务队列数量:0

=========第37次

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第38次

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第39次

执行完毕:pool-1-thread-2

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第40次

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

执行完毕:pool-1-thread-3

=========第41次

线程池中正在执行的线程数量:3

正在执行任务:pool-1-thread-2

线程池缓存的任务队列数量:0

=========第42次

执行完毕:pool-1-thread-2

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第43次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第44次

正在执行任务:pool-1-thread-3

正在执行任务:pool-1-thread-2

执行完毕:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第45次

执行完毕:pool-1-thread-2

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第46次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:1

=========第47次

执行完毕:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第48次

正在执行任务:pool-1-thread-3

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第49次

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:1

=========第50次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:2

=========第51次

执行完毕:pool-1-thread-3

线程池中正在执行的线程数量:3

执行完毕:pool-1-thread-1

执行完毕:pool-1-thread-2

正在执行任务:pool-1-thread-1

线程池缓存的任务队列数量:2

=========第52次

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:1

=========第53次

执行完毕:pool-1-thread-3

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-2

正在执行任务:pool-1-thread-1

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第54次

执行完毕:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

执行完毕:pool-1-thread-1

执行完毕:pool-1-thread-3

=========第55次

正在执行任务:pool-1-thread-2

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

执行完毕:pool-1-thread-2

=========第56次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:1

=========第57次

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第58次

执行完毕:pool-1-thread-2

正在执行任务:pool-1-thread-2

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第59次

执行完毕:pool-1-thread-2

执行完毕:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第60次

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第61次

正在执行任务:pool-1-thread-1

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

执行完毕:pool-1-thread-1

执行完毕:pool-1-thread-2

=========第62次

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第63次

执行完毕:pool-1-thread-1

线程池中正在执行的线程数量:3

正在执行任务:pool-1-thread-1

线程池缓存的任务队列数量:0

=========第64次

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第65次

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第66次

执行完毕:pool-1-thread-2

线程池中正在执行的线程数量:3

正在执行任务:pool-1-thread-2

线程池缓存的任务队列数量:0

=========第67次

线程池中正在执行的线程数量:3

执行完毕:pool-1-thread-2

正在执行任务:pool-1-thread-1

线程池缓存的任务队列数量:0

=========第68次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

正在执行任务:pool-1-thread-3

执行完毕:pool-1-thread-1

=========第69次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第70次

正在执行任务:pool-1-thread-2

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-1

执行完毕:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第71次

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第72次

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第73次

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第74次

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

执行完毕:pool-1-thread-2

线程池缓存的任务队列数量:0

=========第75次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第76次

正在执行任务:pool-1-thread-1

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第77次

执行完毕:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第78次

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第79次

执行完毕:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第80次

正在执行任务:pool-1-thread-2

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第81次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:1

=========第82次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:2

=========第83次

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-3

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:2

=========第84次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:3

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-1

执行完毕:pool-1-thread-2

=========第85次

执行完毕:pool-1-thread-1

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第86次

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:1

=========第87次

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:2

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-1

=========第88次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:2

=========第89次

执行完毕:pool-1-thread-2

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:2

=========第90次

执行完毕:pool-1-thread-1

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-1

执行完毕:pool-1-thread-2

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第91次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:1

=========第92次

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-3

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:1

=========第93次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:2

执行完毕:pool-1-thread-1

执行完毕:pool-1-thread-2

正在执行任务:pool-1-thread-2

正在执行任务:pool-1-thread-1

=========第94次

执行完毕:pool-1-thread-2

正在执行任务:pool-1-thread-2

执行完毕:pool-1-thread-3

执行完毕:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第95次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:1

=========第96次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:2

=========第97次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:2

=========第98次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:3

=========第99次

正在执行任务:pool-1-thread-1

执行完毕:pool-1-thread-2

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:2

=========第100次

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-1

正在执行任务:pool-1-thread-3

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-1

执行完毕:pool-1-thread-2

执行完毕:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-2

=========第101次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第102次

正在执行任务:pool-1-thread-3

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

执行完毕:pool-1-thread-2

执行完毕:pool-1-thread-1

执行完毕:pool-1-thread-3

线程池缓存的任务队列数量:0

=========第103次

线程池中正在执行的线程数量:3

正在执行任务:pool-1-thread-2

线程池缓存的任务队列数量:0

=========第104次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:1

正在执行任务:pool-1-thread-1

执行完毕:pool-1-thread-2

=========第105次

执行完毕:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第106次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:1

=========第107次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:0

=========第108次

正在执行任务:pool-1-thread-1

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:1

=========第109次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:2

=========第110次

正在执行任务:pool-1-thread-3

正在执行任务:pool-1-thread-2

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:2

=========第111次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:3

=========第112次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:4

=========第113次

执行完毕:pool-1-thread-2

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-1

正在执行任务:pool-1-thread-2

线程池中正在执行的线程数量:3

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-3

线程池缓存的任务队列数量:3

=========第114次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:3

=========第115次

线程池中正在执行的线程数量:3

线程池缓存的任务队列数量:4

=========第116次

线程池中正在执行的线程数量:3         由于corePoolSize已经满了,并且缓冲队列也满了,所以此时开始增加线程数,之前一直是1,2,3号线程,现在出现4号线程

线程池缓存的任务队列数量:5

=========第117次

执行完毕:pool-1-thread-2

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-2

正在执行任务:pool-1-thread-1

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-3

线程池中正在执行的线程数量:4

线程池缓存的任务队列数量:2

=========第118次

线程池中正在执行的线程数量:4

线程池缓存的任务队列数量:3

=========第119次

线程池中正在执行的线程数量:4

线程池缓存的任务队列数量:4

=========第120次

线程池中正在执行的线程数量:4

线程池缓存的任务队列数量:5

=========第121次

正在执行任务:pool-1-thread-4

执行完毕:pool-1-thread-1

正在执行任务:pool-1-thread-1

执行完毕:pool-1-thread-2

正在执行任务:pool-1-thread-2

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-3

执行完毕:pool-1-thread-4

正在执行任务:pool-1-thread-4

线程池中正在执行的线程数量:5

线程池缓存的任务队列数量:1

=========第122次

线程池中正在执行的线程数量:5

线程池缓存的任务队列数量:2

正在执行任务:pool-1-thread-5

=========第123次

执行完毕:pool-1-thread-3

线程池中正在执行的线程数量:5

线程池缓存的任务队列数量:2

=========第124次

线程池中正在执行的线程数量:5

线程池缓存的任务队列数量:3

=========第125次

线程池中正在执行的线程数量:5

线程池缓存的任务队列数量:4

=========第126次

线程池中正在执行的线程数量:5

正在执行任务:pool-1-thread-3

执行完毕:pool-1-thread-2

线程池缓存的任务队列数量:5

=========第127次

线程池中正在执行的线程数量:5       由于已达到maxPoolSize已经满了,并且缓冲队列也达到最大值,所以此时新增的任务被驳回,异常见下方。

线程池缓存的任务队列数量:5

=========第128次

正在执行任务:pool-1-thread-2

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-3

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-3

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-3

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-3

执行完毕:pool-1-thread-3

正在执行任务:pool-1-thread-3

执行完毕:pool-1-thread-3

执行完毕:pool-1-thread-1

执行完毕:pool-1-thread-2

执行完毕:pool-1-thread-5

执行完毕:pool-1-thread-4

Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task stone.ripple.util.threadpool.MyTask@42a57993 rejected from java.util.concurrent.ThreadPoolExecutor@75b84c92[Running, pool size = 5, active threads = 5, queued tasks = 5, completed tasks = 118]

at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)

at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)

at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)

at stone.ripple.util.threadpool.ThreadPoolTest.main(ThreadPoolTest.java:30)

执行中:Thread-1

执行中:Thread-4

执行中:Thread-7

执行中:Thread-8

执行中:Thread-9

执行中:Thread-10

执行中:Thread-11

执行中:Thread-12

执行中:Thread-14

执行中:Thread-16

执行中:Thread-15

执行中:Thread-17

执行中:Thread-18

执行中:Thread-20

执行中:Thread-19

执行中:Thread-21

执行中:Thread-5

执行中:Thread-26

执行中:Thread-0

执行中:Thread-2

执行中:Thread-3

执行中:Thread-25

执行中:Thread-24

执行中:Thread-22

执行中:Thread-23

执行中:Thread-13

执行中:Thread-6

执行中:Thread-29

执行中:Thread-31

执行中:Thread-28

执行中:Thread-30

执行中:Thread-33

执行中:Thread-34

执行中:Thread-27

执行中:Thread-35

执行中:Thread-32

执行中:Thread-38

执行中:Thread-37

执行中:Thread-41

执行中:Thread-39

执行中:Thread-36

执行中:Thread-43

执行中:Thread-44

执行中:Thread-45

执行中:Thread-57

执行中:Thread-46

执行中:Thread-58

执行中:Thread-75

执行中:Thread-47

执行中:Thread-74

执行中:Thread-59

执行中:Thread-48

执行中:Thread-76

执行中:Thread-60

执行中:Thread-49

执行中:Thread-50

执行中:Thread-61

执行中:Thread-62

执行中:Thread-51

执行中:Thread-63

执行中:Thread-53

执行中:Thread-66

执行中:Thread-52

执行中:Thread-77

执行中:Thread-67

执行中:Thread-54

执行中:Thread-68

执行中:Thread-55

执行中:Thread-65

执行中:Thread-56

执行中:Thread-64

执行中:Thread-42

执行中:Thread-69

执行中:Thread-78

执行中:Thread-70

执行中:Thread-72

执行中:Thread-71

执行中:Thread-73

执行中:Thread-40

执行中:Thread-79

执行中:Thread-82

执行中:Thread-83

执行中:Thread-85

执行中:Thread-86

执行中:Thread-88

执行中:Thread-90

执行中:Thread-81

执行中:Thread-80

执行中:Thread-98

执行中:Thread-97

执行中:Thread-100

执行中:Thread-96

执行中:Thread-99

执行中:Thread-94

执行中:Thread-101

执行中:Thread-93

执行中:Thread-110

执行中:Thread-102

执行中:Thread-103

执行中:Thread-109

执行中:Thread-111

执行中:Thread-105

执行中:Thread-113

执行中:Thread-104

执行中:Thread-91

执行中:Thread-112

执行中:Thread-106

执行中:Thread-115

执行中:Thread-114

执行中:Thread-107

执行中:Thread-118

执行中:Thread-117

执行中:Thread-92

执行中:Thread-108

执行中:Thread-89

执行中:Thread-87

执行中:Thread-84

执行中:Thread-95

执行中:Thread-123

执行中:Thread-124

执行中:Thread-125

执行中:Thread-126

执行中:Thread-127

执行中:Thread-122

执行中:Thread-121

执行中:Thread-119

执行中:Thread-120

执行中:Thread-116

 

四、总结:

      1. ThreadPoolExecutor从execute方法进入执行任务,当线程数量小于核心线程数量时,增加worker执行任务;

      2. worker进入runWork方法执行,任务执行完成后,会调用getTask获取新任务,如果不能获取到新任务,有以下情况:

   【如果已超过corePoolSize并且允许超时,且在规定时间内一直获取不到新任务worker数量会递减;

      如果已超过maxPoolSize并且缓存队列为空worker数量会递减;

      如果能获取到新任务以当前线程继续执行任务;

       3. 如果execute时发现线程数量已经达到corePoolSize,则会将任务加入缓冲队列;

       4. 如果excute时发现corePoolSize已经达到最大值,并且缓冲队列也已经达到最大值,则会增加worker,直到maxPoolSize。

       5. 如果excute时发现maxPoolSize已达最大值,并且缓冲队列也已达到最大值,则会拒绝任务。

 

 

posted @ 2017-03-10 17:08  sky_wild  阅读(5734)  评论(0编辑  收藏  举报