java - ThreadPoolExecutor 中的同步队列

标签 java multithreading threadpool threadpoolexecutor blockingqueue

我试图了解 ThreadPoolExecutor 中队列的行为。在下面的程序中,当我使用 LinkedBlockingQueue 时,我一次只能向线程池提交一个任务。但是,如果我将 LinkedBlockingQueue 替换为 SynchronousQueue,我可以立即将所有 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();
        }
    }
}

最佳答案

当您向 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)时,只有在 另一个线程正在等待接收它。因为没有等待 thread, false 将被返回并且新线程被创建 时间。

关于java - ThreadPoolExecutor 中的同步队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47650247/

相关文章:

java - 是否可以通过 Google App Engine 中的光标确定您在结果集中的位置?

Python:我应该子类化 threading.Thread 吗?

multithreading - JDK 中的异步 NIO 是如何工作的?

java - scheduleWithFixedDelay 抛出异常如何重启schedule?

multithreading - 计算最小值的最短时间

java - 文件并行处理

java - 在android中创建动态微调器

JavaFX 检测舞台屏幕位置的变化

java - Java 中的静态变量启动器

c# - 使用任务并行库时得到奇怪的结果?