JAVA并发编程:线程池 ThreadPoolExecutor

生活

前期追深度,否则会华而不实,后期追广度,否则会坐井观天;

前言

在前面,我们已经对Thread有了比较深入的了解,并且已经学会了通过new Thread()来创建一个线程,并通过start方法来启动一个线程,这种方法非常简单,同样也存在弊端:
1、每次通过new Thread()创建对象性能不佳
2、线程缺乏统一管理,可能无限创建线程,相互竞争,极端情况下回出现OOM
3、无法提供定时执行、定期执行
所以在企业级项目里,用到线程的地方,大多有线程池的介入。

ThreadPoolExecutor的成员

    private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));

用来表示线程池的状态以及正在运行的线程数。其中高3位描述线程池的状态,低29位用来表示正在运行的线程数。当线程池创建时,默认的状态是Running,默认的正在运行的线程数为0.
线程池的状态有以下几种:
RUNNING:允许已经和执行新任务
SHUTDOWN:不允许提交新任务,会执行已经提交到队列的任务
STOP:不允许提交新任务,也不执行在队列等待的任务,设置正在执行的任务的中断标志位
DITYING:所有任务执行完毕,线程池中的工作线程为0,等待执行钩子方法terminated
TERMINATED:钩子方法执行完毕

注意这里的状态指的是线程池的状态,并不是指线程池中的线程状态。
执行shutdown方法,可以使线程池的状态由RUNNING转为SHUTDOWN;
执行shutdownNow方法,可以使线程池的状态由RUNNING转为STOP
SHUTDOWN和STOP都会先转为DITYING,再转为TERMINATED.

以下成员对线程池的性能有很大影响,放在构造器里详说。

    private final BlockingQueue<Runnable> workQueue;
    private volatile int maximumPoolSize;
    private volatile long keepAliveTime;
    private volatile ThreadFactory threadFactory;
    private volatile RejectedExecutionHandler handler;
    private volatile int corePoolSize;
    private volatile int maximumPoolSize;

//线程池创建至今 最大的线程数
private int largestPoolSize;
//已完成的任务数
private long completedTaskCount;
//是否允许在空闲时销毁核心线程
private volatile boolean allowCoreThreadTimeOut;
//工作线程的集合
private final HashSet<Worker> workers = new HashSet<Worker>();

ThreadPoolExecutor 构造函数

//所有构造函数最终都调用到这个构造器,来看下他的参数
 public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.acc = System.getSecurityManager() == null ?
                null :
                AccessController.getContext();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

corePoolSize:线程池中核心线程数的最大值
maximumPoolSize :线程池中最多的线程数
workQueue:用来缓存任务的阻塞队列
用一个添加新任务的场景来描述上面三者的关系
1、如果没有空闲线程且线程数小于核心线程数,就创建一个新的线程执行
2、如果没有空闲线程且线程数等于核心线程数,就把任务缓存到阻塞队列
3、如果没有空闲线程且线程数小于最大线程数且阻塞队列已满,则创建一个新的线程执行
4、如果没有空闲线程且线程数大于最大线程数且阻塞队列已满,则根据构造函数传入的拒绝策略做出相应操作。

keepAliveTime:空闲超过这个时间的线程会被销毁【maximumPoolSize为true时,核心线程也由这个时间控制销毁】
unit:上面超时时间的单位
threadFactory:创建线程的工厂
handler:拒绝策略
拒绝策略有以下四种
1、AbortPolicy:直接抛出异常
2、CallerRunsPolicy:用线程池提交任务的线程去执行 直接run
3、DiscardOldestPolicy:丢弃最早进入队列没有执行的线程
4、DiscardPolicy:丢弃这个线程

线程池的提交

线程池的submit execute最终都执行下面的方法来提交线程

 public void execute(Runnable command) {
 //判空
        if (command == null)
            throw new NullPointerException();
    
        int c = ctl.get();
        //当工作线程数小于核心线程时
        if (workerCountOf(c) < corePoolSize) {
        //添加新的工作线程
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        
	//判断线程池时运行中状态, 并加入到等待队列
        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);
        }
        //添加核心线程以外的工作线程来执行。失败就调用拒绝策略。
        else if (!addWorker(command, false))
            reject(command);
    }
-------
 private boolean addWorker(Runnable firstTask, boolean core) {
        retry:
        for (;;) {
            int c = ctl.get();
            int rs = runStateOf(c);

            //这个判断看了好久。。注意,,如果你一定要把两个判断条件一起看,真的不太好理解
            //我是这么理解的
            //if里面两个条件:
            //1、rs >= SHUTDOWN
            //2、  ! (rs == SHUTDOWN &&
                   firstTask == null &&
                   ! workQueue.isEmpty())
            //中间用&连接,也就是1不成立或者2不成立才会不进if里面,往下走
	   // 也就是  rs<SHUTDOWN为true 即 运行中
	   //或者 rs == SHUTDOWN &&
                   firstTask == null &&
                   ! workQueue.isEmpty()  即SHUTDOWN状态,并且当前没有提交新任务,并且等待队列非空
//连起来就得知 新建一个工作线程的 条件是   运行中 或者 (SHUTDOWN状态,等待队列非空,不提交新任务)
//满足这些条件,才能往下走

            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;
                    //cas新增工作线程数
                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());
			
			//如果运行中 或者  已经SHUTDOWN,但是没有提交新任务,,这个写法就友好多了,一目了然
                    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;
    }

线程池的执行

线程池的执行看Work的run方法,run调用到runWork

 final void runWorker(Worker w) {
        Thread wt = Thread.currentThread();
        Runnable task = w.firstTask;
        w.firstTask = null;
        w.unlock(); // allow interrupts
        boolean completedAbruptly = true;
        try {
        //work里的firstTask获取队列里的task不为空,就往下去执行
            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);
        }
    }

看一下 getTask方法如何从队列中得到任务呢

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;
            (
		//当前线程数 大于最大线程数 或者
		//已经超时
		)
		并且
		(
                当前工作线程数大于1 
                获取等待队列已经空了
               )
               就尝试缩减工人
	
		
            if ((wc > maximumPoolSize || (timed && timedOut))
                && (wc > 1 || workQueue.isEmpty())) {
                if (compareAndDecrementWorkerCount(c))
                    return null;
                continue;
            }

          //如果设置超时了,就要poll超时获取
          //否则用take,无限期阻塞
            try {
                Runnable r = timed ?
                    workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
                    workQueue.take();
                if (r != null)
                    return r;
                timedOut = true;
            } catch (InterruptedException retry) {
                timedOut = false;
            }
        }
    }

线程池的关闭

 public void shutdown() {
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            checkShutdownAccess();
            // 更新状态
            advanceRunState(SHUTDOWN);
            //尝试中断worker,调用interruptIdleWorkers传入false;
            interruptIdleWorkers();
            onShutdown(); // hook for ScheduledThreadPoolExecutor
        } finally {
            mainLock.unlock();
        }
        tryTerminate();
    }
private void interruptIdleWorkers(boolean onlyOne) {
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            for (Worker w : workers) {
                Thread t = w.thread;
                // 如果线程还没有中断 并且能够获得Worker 的锁,说明已经执行完了,就可以中断到
//奇怪,不知道为啥这个worker没有去继承可重入锁,而是写了一模一样的代码进去。。。。
                if (!t.isInterrupted() && w.tryLock()) {
                    try {
                        t.interrupt();
                    } catch (SecurityException ignore) {
                    } finally {
                        w.unlock();
                    }
                }
                if (onlyOne)
                    break;
            }
        } finally {
            mainLock.unlock();
        }
    }

后记

明天来研究下Executors给我们提供的几个默认的ThreadPoolExecutor

猜你喜欢

转载自blog.csdn.net/qq_28605513/article/details/84932937