Java并发之线程池

为什么需要线程池

操作系统中线程的实现有三种,一种是用户级线程,一种是内核支持线程,还有一种是前两种的组合方式。用户级线程是在用户空间实现的,而内核级线程是在OS内核空间实现的。JVM对于线程并没有明确的定义是用户线程还是内核线程,但Java常用的JVM HotSpot,它都是使用1:1线程模型即内核线程,线程的调度完全交给了操作系统内核;所以在HotSpot上创建线程需要操作系统从用户态切换到内核态,这个开销是巨大的。而Java的线程在使用完后就会被回收,而需要时又会被创建,所以通过将空闲线程管理起来成为线程池,当需要线程运行任务的时候就从线程池中拿线程,避免了线程的创建过程,提升效率。

线程池构造函数

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 当提交一个任务到线程池时,线程池会创建一个线程来执行任务,即使其他空闲的线程能够执行新任务也会创建线程,等到需要执行的任务数大于corePoolSize时就不再创建。如果调用了线程池的prestartAllCoreThreads()方法,线程池会提前创建并启动所有corePoolSize线程
maximumPoolSize 线程池允许创建的最大线程数。如果队列满了,并且已创建的线程数小于最大线程数,则线程池会再创建新的线程执行任务
keepAliveTime 线程活动保持时间,线程池的工作线程空闲后,保持存活的时间
unit 线程活动保持时间的单位
workQueue 任务队列,用于保存等待执行的任务的阻塞队列
threadFactory 创建线程时使用的线程创建工厂
handler 拒绝策略,当队列和线程池都满了,说明线程池处于饱和状态,那么必须采取一种策略处理提交的新任务。这个策略默认情况下是AbortPolicy,表示无法处理新任务时抛出异常。

线程池源码分析

向线程池提交任务有两种方法,分别是使用execute()和submit()方法提交:

public void execute(Runnable command) {
    if (command == null)
        throw new NullPointerException();
    // 获取线程池状态        
    int c = ctl.get();
    // 判断线程池运行的线程数是否小于核心线程数
    if (workerCountOf(c) < corePoolSize) {
        // 小于核心线程数,将任务包装成worker
        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);
    }
    // 核心线程数已满且阻塞队列已满,创建非核心线程Worker
    else if (!addWorker(command, false))
        // 创建非核心线程Worker失败,执行拒绝策略
        reject(command);
}

public <T> Future<T> submit(Callable<T> task) {
    if (task == null) throw new NullPointerException();
    RunnableFuture<T> ftask = newTaskFor(task);
    execute(ftask);
    return ftask;
}

public Future<?> submit(Runnable task) {
    if (task == null) throw new NullPointerException();
    RunnableFuture<Void> ftask = newTaskFor(task, null);
    execute(ftask);
    return ftask;
}

public <T> Future<T> submit(Runnable task, T result) {
    if (task == null) throw new NullPointerException();
    RunnableFuture<T> ftask = newTaskFor(task, result);
    execute(ftask);
    return ftask;
}

从上面源码可以看到,提交给线程池的任务都会被线程池通过addWorker()方法包装成一个Worker内部类,Worker内部类及addWorker()方法源码如下:

private final class Worker
    extends AbstractQueuedSynchronizer
    implements Runnable
{
    private static final long serialVersionUID = 6138294804551838833L;

    final Thread thread;
    Runnable firstTask;
    volatile long completedTasks;

    Worker(Runnable firstTask) {
        setState(-1); // inhibit interrupts until runWorker
        this.firstTask = firstTask;
        this.thread = getThreadFactory().newThread(this);
    }

    public void run() {
        runWorker(this);
    }

    protected boolean isHeldExclusively() {
        return getState() != 0;
    }

    protected boolean tryAcquire(int unused) {
        if (compareAndSetState(0, 1)) {
            setExclusiveOwnerThread(Thread.currentThread());
            return true;
        }
        return false;
    }

    protected boolean tryRelease(int unused) {
        setExclusiveOwnerThread(null);
        setState(0);
        return true;
    }

    public void lock()        { acquire(1); }
    public boolean tryLock()  { return tryAcquire(1); }
    public void unlock()      { release(1); }
    public boolean isLocked() { return isHeldExclusively(); }

    void interruptIfStarted() {
        Thread t;
        if (getState() >= 0 && (t = thread) != null && !t.isInterrupted()) {
            try {
                t.interrupt();
            } catch (SecurityException ignore) {
            }
        }
    }
}

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()))
        // rs >= SHUTDOWN,线程池状态为STOP, TIDYING, 或TERMINATED,不再接收新任务
        // rs = SHUTDOWN 并且firstTask为空,并且workQueue非空,是允许创建worker,表示继续处理队列中的任务
        return false;

    for (;;) {
        int wc = workerCountOf(c);
        // 校验传入的线程数是否超过了容量大小, 或者是否超过了corePoolSize或maximumPoolSize
        if (wc >= CAPACITY ||
            wc >= (core ? corePoolSize : maximumPoolSize))
            return false;
        // 线程数没有超,那么就用CAS将线程池的个数加1
        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 {
    // 创建一个worker
    w = new Worker(firstTask);
    final Thread t = w.thread;
    if (t != null) {
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            int rs = runStateOf(ctl.get());
            // 判断线程池状态和线程状态,并加入workers队列
            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;
}

addWorker先对线程池状态及线程池大小做校验,随后将任务包装成一个Worker,Worker是一个实现了Runnable的内部类,run()方法直接调用到ThreadPoolExecutor的runWorker方法

final void runWorker(Worker w) {
    Thread wt = Thread.currentThread();
    // 拿到worker中实际需要执行的任务
    Runnable task = w.firstTask;
    w.firstTask = null;
    w.unlock(); // allow interrupts
    boolean completedAbruptly = true;
    try {
        // 如果任务为空,则从workQueue里面获取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);
    }
}

线程池状态管理

线程池状态管理通过一个32位的整数来存放线程池的状态和当前池中的线程数,其中高3位用于存放线程池状态,低29位表示线程数,源码如下

private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
// COUNT_BITS 设置为 29(32-3),意味着前三位用于存放线程状态,后29位用于存放线程数
private static final int COUNT_BITS = Integer.SIZE - 3;
// 最大线程数是 2^29-1=536870911
private static final int CAPACITY   = (1 << COUNT_BITS) - 1;

// runState is stored in the high-order bits
// 线程池状态
// 111 00000000000000000000000000000,接受新的任务,处理等待队列中的任务
private static final int RUNNING    = -1 << COUNT_BITS;
// 000 00000000000000000000000000000,不接受新的任务提交,但是会继续处理等待队列中的任务
private static final int SHUTDOWN   =  0 << COUNT_BITS;
// 001 00000000000000000000000000000,不接受新的任务提交,不再处理等待队列中的任务,中断正在执行任务的线程
private static final int STOP       =  1 << COUNT_BITS;
// 010 00000000000000000000000000000,所有的任务都销毁了,workCount为0,线程池的状态在转换为 TIDYING 状态时,会执行钩子方法 terminated()
private static final int TIDYING    =  2 << COUNT_BITS;
// 011 00000000000000000000000000000,terminated() 方法结束后,线程池的状态就会变成这个状态
private static final int TERMINATED =  3 << COUNT_BITS;

// Packing and unpacking ctl
// 线程池状态获取方法,将CAPACITY取非后和c进行取与运算,可以得到高3位的值,即线程池的状态
private static int runStateOf(int c)     { return c & ~CAPACITY; }
// 线程池线程个数获取方法,将c和CAPACITY取与运算,可以得到低29位的值,即线程池的个数
private static int workerCountOf(int c)  { return c & CAPACITY; }
private static int ctlOf(int rs, int wc) { return rs | wc; }
posted @ 2020-11-05 23:25  Sirius-  阅读(94)  评论(0编辑  收藏  举报