线程池 ThreadPoolExecutor 方法中的 execute方法源码解析

public void execute(Runnable command) {
    
    
        if (command == null)
            throw new NullPointerException();
		//ctl 32位的integer的数字,前3位是线程池的状态,后29位是线程池内的线程数量
        int c = ctl.get();
        // 如果正在运行的线程数小于核心线程数,直接加一个线程
        if (workerCountOf(c) < corePoolSize) {
    
    
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        //如果正在运行的线程数大于核心线程数
        //直接将任务添加到队列中
        //offer()方法实际上并不是阻塞的,非阻塞的如果此时队列满,不是卡住等待人家take掉一个元素,而是直接返回false
        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);
    }

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;
                    //尝试用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 {
    
    
        	//创建work
            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();
                        //将创建的Worker 加上一个 HashSet的集合中
                        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;
    }

猜你喜欢

转载自blog.csdn.net/itlijinping_zhang/article/details/120415501