线程池执行器中的同步队列 (SynchronousQueue)

9
我正在尝试理解ThreadPoolExecutor中队列的行为。在下面的程序中,当我使用LinkedBlockingQueue时,我只能一次向线程池提交一个任务。但是如果我用SynchronousQueue代替LinkedBlockingQueue,我可以立即将所有5个任务提交到池中。在这种情况下,SynchronousQueueLinkedBlockingQueue有何不同?

Java程序:

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Sample {
    public static void main(String[] args) throws InterruptedException {
        LinkedBlockingQueue<Runnable> threadPoolQueue = new LinkedBlockingQueue<>();
//      SynchronousQueue<Runnable> threadPoolQueue = new SynchronousQueue<>();
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        ThreadPoolExecutor tpe = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, threadPoolQueue, threadFactory);
        Runnable np;

        for (int i = 1; i <= 5; i++) {
            np = new SampleWorker("ThreadPoolWorker " + i);
            tpe.submit(np);
        }

        System.out.println(tpe.getCorePoolSize());
        System.out.println(tpe.getPoolSize());
        System.out.println(tpe.getActiveCount());

        tpe.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
        tpe.shutdown();
        System.out.println("Main task finished");
    }
}

class SampleWorker implements Runnable {
    private String workerName;

    SampleWorker(String tName) {
        workerName = tName;
    }

    @Override
    public void run() {
        try {
            for (int i = 1; i <= 10; i++) {
                Thread.sleep(3000);
                System.out.println(this.workerName);
            }
            System.out.println(this.workerName + " finished");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

1
请看这里 - Dawid Fieluba
@avix 这解释了 SynchronousQueueLinkedBlockingQueue(大小为1)之间的区别。但是在这里,我没有为任何一个队列指定大小。使用 LinkedBlockingQueue,我一次只能提交一个任务,而使用 SynchronousQueue,我可以同时提交所有5个任务。在两种情况下,我都将 corePoolSize 设置为0。那么 corePoolSize 不会影响 SynchronousQueue 吗? - UnahD
@UnahD corePoolSize 不影响底层的 BlockingQueue,它仅控制 ThreadPoolExecutor 的线程池大小,而不是 BlockingQueue 的大小。并且,正如文档所说,SynchronousQueue 没有任何内部容量,甚至没有一个容量size()始终返回零 - Maxim Ponomarev
1个回答

18

当您向ThreadPoolExecutor提交任务时,它的工作方式如下:

if (numberOfWorkingThreads < corePoolSize) {
   startNewThreadAndRunTask();
} else if (workQueue.offer(task)) {
   if (numberOfWorkingThreads == 0) {
       startNewThreadAndRunTask();
   }
} else if (numberOfWorkingThreads < maxPoolSize)
    startNewThreadAndRunTask();
} else {
    rejectTask();
}
  • 当使用没有初始值的LinkedBlockingQueue时,workQueue.offer(task)总是成功的,导致只有一个线程被启动。
  • 当调用SynchronousQueue.offer(task)时,仅当另一个线程正在等待接收任务时,它才会成功。由于没有等待线程,每次都会返回false并创建新的线程。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接