Oracle定义newFixedThreadPool(1)方法如下,
Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue
我可以设置固定的队列大小,例如1,这样我就可以阻止正在处理的新任务,直到当前任务执行完成,甚至使用堆栈而不是队列,当及时工作时,第一个任务可能会在一段时间后无效,因此可能需要固定大小的堆栈。
最佳答案
你不能。 Executors
提供了一些常用的静态工厂方法,例如
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
但如果这不是您所需要的,请创建您自己的ThreadPoolExecutor
。例如
public static ExecutorService newFixedThreadPoolWithBoundQueue(
int nThreads,
int capacity) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(capacity));
}
or even use stack instead of queue
我不会尝试这样做。
关于java - 如何配置 ExecutorService newFixedThreadPool() 行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36082946/