子衿沉夜
您需要将 PriorityBlockingQueue 传递给ThreadPoolExecutor,可能使用此构造函数。在下面添加了如何使用比较器初始化 PriorityBlockingQueue 的示例public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler);}编辑 - 将比较器添加到优先级队列的示例import java.util.Comparator;import java.util.concurrent.PriorityBlockingQueue;import java.util.concurrent.BlockingQueue;public class PriorityBlockQueueTest { private static BlockingQueue<PriorityTask> taskQueue = new PriorityBlockingQueue<>(10, new Comparator<PriorityTask>() { @Override public int compare(PriorityTask o1, PriorityTask o2) { return o2.getData() - o1.getData(); } }); public static void main(String arg[]) { taskQueue.add(new PriorityTask(2, 10)); taskQueue.add(new PriorityTask(1, 11)); System.out.println(taskQueue); } private static class PriorityTask implements Runnable { private int priority; private int data; public PriorityTask(int priority, int data) { this.priority = priority; this.data = data; } public int getData() { return data; } public void run() { System.out.println("Running something"); } public String toString() { return "priority: " + priority + " data: " + data; } }}