Seçenek 1:
newWorkStealingPool danExecutors
public static ExecutorService newWorkStealingPool()
Hedef paralellik düzeyi olarak tüm kullanılabilir işlemcileri kullanarak bir iş hırsızlığı iş parçacığı havuzu oluşturur.
Bu API ile çekirdek sayısını aktarmanıza gerek yoktur ExecutorService.
Bu API'nin grepcode'dan uygulanması
/**
* Creates a work-stealing thread pool using all
* {@link Runtime#availableProcessors available processors}
* as its target parallelism level.
* @return the newly created thread pool
* @see #newWorkStealingPool(int)
* @since 1.8
*/
public static ExecutorService newWorkStealingPool() {
return new ForkJoinPool
(Runtime.getRuntime().availableProcessors(),
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, true);
}
Seçenek 2:
newFixedThreadPool API'si Executorsveya other newXXX constructorsdöndürürExecutorService
public static ExecutorService newFixedThreadPool(int nThreads)
nThreads değiştir Runtime.getRuntime().availableProcessors()
3. Seçenek:
ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue)
geçmesi Runtime.getRuntime().availableProcessors()için parametre olarak maximumPoolSize.