`
flyfoxs
  • 浏览: 297688 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

ThreadPoolExecutor中largestPoolSize和maximumPoolSize的区别

 
阅读更多

查看ThreadPoolExecutor的源代码时,发现了2个意思十分接近的变量(maximumPoolSize, largestPoolSize) .十分好奇,google,百度也无法得到答案.只好查看源码,现将结果分享出来.

 

maximumPoolSize:是一个静态变量,在变量初始化的时候,有构造函数指定.

 

largestPoolSize: 是一个动态变量,是记录Poll曾经达到的最高值,也就是 largestPoolSize<= maximumPoolSize.

 

 

分析过程:

 maximumPoolSize

从构造函数就可以看到,这个值在构造函数初始化时,直接指定的.(当然后期可以修改,但是这个应该属于高级用法)

 

  public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), defaultHandler);
    }

 

 

 

largestPoolSize

整个Class只有这个方法里面这段高亮的代码修改了这个变量.可以很直接的看到, 是看看当前work的个数是和历史值取最大值比较,然后取最大值赋值给largestPoolSize.

并且largestPoolSize的JavaDoc也可以很容易的验证这个逻辑(其实最初是没看懂,了解业务后回头来看还是比较好理解)

Tracks largest attained pool size. Accessed only under mainLock.

 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();
                     
                        workerAdded = true;
                    }
                } finally {
                    mainLock.unlock();
                }
                if (workerAdded) {
                    t.start();
                    workerStarted = true;
                }
            }
        } finally {
            if (! workerStarted)
                addWorkerFailed(w);
        }
        return workerStarted;
    }

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics