ThreadPoolTask​​Executor bean 中的同步队列公平策略?

Synchronous Queue fairness policy in ThreadPoolTaskExecutor bean?

我有以下bean

<bean id="executor"
    class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="1" />
    <property name="maxPoolSize" value="1" />
    <!-- Positive value leads to LinkedBlockingQueue,
        any other value leads to SynchronousQueue -->
    <property name="queueCapacity" value="0" />
</bean>

Oracle documentation for SynchronousQueue 表示如下:

This class supports an optional fairness policy for ordering waiting producer and consumer threads. By default, this ordering is not guaranteed. However, a queue constructed with fairness set to true grants threads access in FIFO order.

我的问题是:如何在我的 bean 配置中指定我想要顺序公平?

谢谢!

我不确定为什么您需要一个公平的同步队列并按 FIFO 顺序管理线程访问。我从未真正见过这种情况,也不确定即使您管理 FIFO,您的应用程序是否也能正常工作。

但如果您愿意,您仍然可以扩展 ThreadPoolTaskExecutor 并像这样覆盖 createQueue() -

public class FairThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
    protected BlockingQueue<Runnable> createQueue(int queueCapacity) {
        return (BlockingQueue) (queueCapacity > 0 ? new LinkedBlockingQueue(queueCapacity) : new SynchronousQueue(true));
    }
}