我在使用Java 6的ThreadPoolExecutor时遇到了一个奇怪的问题。当我不时动态地更改corePoolSize时,我观察到线程池未处理应做的任务。

例如,如果我有4个corePoolSize且队列中有许多任务等待执行,则执行程序最多只能处理3个,有时甚至要处理2个。

在调查问题时,我注意到当我增加或减少corePoolSize时,我从未更改过maxPoolSize。从我的应用程序开始,它始终为1。

在Java文档中从未发现有声明提到maxPoolSize的作用小于核心。

然后,当我检查源代码时,我注意到在costructor和setCorePoolSize方法中,都会检查是否在maximumPoolSize小于corePoolSize的情况下抛出异常ArgumentException。查看以下代码。

建设者

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.corePoolSize = corePoolSize;
    this.maximumPoolSize = maximumPoolSize;
    this.workQueue = workQueue;
    this.keepAliveTime = unit.toNanos(keepAliveTime);
    this.threadFactory = threadFactory;
    this.handler = handler;
}


设置最大池大小

public void setMaximumPoolSize(int maximumPoolSize) {
    if (maximumPoolSize <= 0 || maximumPoolSize < corePoolSize)
        throw new IllegalArgumentException();
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        int extra = this.maximumPoolSize - maximumPoolSize;
        this.maximumPoolSize = maximumPoolSize;
        if (extra > 0 && poolSize > maximumPoolSize) {
            try {
                Iterator<Worker> it = workers.iterator();
                while (it.hasNext() &&
                       extra > 0 &&
                       poolSize > maximumPoolSize) {
                    it.next().interruptIfIdle();
                    --extra;
                }
            } catch (SecurityException ignore) {
                // Not an error; it is OK if the threads stay live
            }
        }
    } finally {
        mainLock.unlock();
    }
}


因此,显然这是不希望的情况。但是在setCorePoolSize中没有检查,这会导致maximumPoolSize最终小于corePoolSize,并且这种情况的影响没有记录。

设置核心池大小

public void setCorePoolSize(int corePoolSize) {
    if (corePoolSize < 0)
        throw new IllegalArgumentException();
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        int extra = this.corePoolSize - corePoolSize;
        this.corePoolSize = corePoolSize;
        if (extra < 0) {
            int n = workQueue.size(); // don't add more threads than tasks
            while (extra++ < 0 && n-- > 0 && poolSize < corePoolSize) {
                Thread t = addThread(null);
                if (t == null)
                    break;
            }
        }
        else if (extra > 0 && poolSize > corePoolSize) {
            try {
                Iterator<Worker> it = workers.iterator();
                while (it.hasNext() &&
                       extra-- > 0 &&
                       poolSize > corePoolSize &&
                       workQueue.remainingCapacity() == 0)
                    it.next().interruptIfIdle();
            } catch (SecurityException ignore) {
                // Not an error; it is OK if the threads stay live
            }
        }
    } finally {
        mainLock.unlock();
    }
}


您不认为应该有一种机制来阻止这种情况的发生吗?

最佳答案

我认为,您是对的,应该进行类似的测试

if (corePoolSize < 0 || corePoolSize > maxPoolSize)
    throw new IllegalArgumentException();


但是您可以在调用setCorePoolSize之前轻松进行测试,并在必要时调整最大池大小。即使在那里进行了这样的测试,您也必须在调用setCorePoolSize之前检查最大池大小,以避免得到IllegalArgumentException ...

08-04 09:57