$(VC _ ExecutablePath _ x86 _ ARM64)
$(VC _ ExecutablePath _ x86 _ x64)
无界问题因为 Execector 前面有一个 SynchronousQueue而变得更加严重,这意味着在任务提供者和线程池之间有一个直接的切换。如果所有现有线程都忙,则每个新任务将创建一个新线程。对于服务器代码来说,这通常是一个糟糕的策略。当 CPU 饱和时,现有的任务需要更长的时间才能完成。然而,提交的任务越来越多,创建的线程也越来越多,因此完成任务的时间越来越长。当 CPU 饱和时,更多的线程肯定不是服务器所需要的。
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,
RejectedExecutionHandler handler)
public ThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler
)
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}