开满天机
选项1:来自的newWorkStealingPoolExecutorspublic static ExecutorService newWorkStealingPool()使用所有可用处理器作为目标并行度级别,创建工作窃取线程池。使用此API,您无需将内核数传递给ExecutorService。通过grepcode实现此API/** * 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); }选项2:来自Executors或的newFixedThreadPool API other newXXX constructors,返回ExecutorServicepublic static ExecutorService newFixedThreadPool(int nThreads)将nThreads替换为 Runtime.getRuntime().availableProcessors()选项3:线程池执行器public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue)Runtime.getRuntime().availableProcessors()作为参数传递给maximumPoolSize。