ThreadPoolExecutor

继续来学习Doug Lea大神的代码

线程池

线程池的状态:
RUNNING: Accept new tasks and process queued tasks
SHUTDOWN: Don’t accept new tasks, but process queued tasks
STOP: Don’t accept new tasks, don’t process queued tasks,
and interrupt in-progress tasks
TIDYING: All tasks have terminated, workerCount is zero,
he thread transitioning to state TIDYING
will run the terminated() hook method

TERMINATED: terminated() has completed
RUNNING -> SHUTDOWN:shutdown()
SHUTDOWN -> TIDYING:队列和线程池均为空
STOP -> TIDYING:线程池为空
TIDYING -> TERMINATED:terminated()

最核心的构造函数:

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler)

corePoolSize:线程池中最大的核心线程的数量,核心线程即为即使处于等待状态也不会被销毁的线程
maximumPoolSize:线程池允许的最大线程数
keepAliveTime:当线程池中线程数量大于核心线程数量时,工作线程的最大等待时间,过了等待时间工作线程会被终止
unit:时间的单位
workQueue:任务被处理之前等待的队列
threadFactory:线程工厂
handler:当线程池和阻塞队列全部满的时候对新任务的处理方式

execute

public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        /*
         * Proceed in 3 steps:
         *
         * 1. If fewer than corePoolSize threads are running, try to
         * start a new thread with the given command as its first
         * task.  The call to addWorker atomically checks runState and
         * workerCount, and so prevents false alarms that would add
         * threads when it shouldn't, by returning false.
         *
         * 2. If a task can be successfully queued, then we still need
         * to double-check whether we should have added a thread
         * (because existing ones died since last checking) or that
         * the pool shut down since entry into this method. So we
         * recheck state and if necessary roll back the enqueuing if
         * stopped, or start a new thread if there are none.
         *
         * 3. If we cannot queue task, then we try to add a new
         * thread.  If it fails, we know we are shut down or saturated
         * and so reject the task.
         */
        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);
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        else if (!addWorker(command, false))
            reject(command);
    }

1、如果线程池内线程数小于corePoolSize,则每来一个任务就新建一个核心线程去处理;
2、如果线程池内线程数小于corePoolSize,小于maximumPoolSize,往阻塞队列插入任务;
3、如果2中插入失败,那么直接添加一个工作线程来处理;
4、如果2中插入成功,等待空闲线程来取任务执行,如果线程池线程数为0,添加一个线程来处理任务;如果线程池不处于运行状态并且任务删除成功,拒绝后面的任务。
在这里插入图片描述

参考资料

jdk 1.8中的线程池 作者: EnjoyCodingEnjoyLife
java6的实现 作者: Matrix海子

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值