当前位置: 首页>后端>正文

线程池:自定义线程池

线程池:自定义线程池,第1张

public class Thread11 {

public static void main(String[] args) {

// 自定义线程池:池的大小如何设置

// 策略:CPU密集型:几核、就是几、可以保持cpu的效率最高

// 获取CPU的核数

System.out.println(Runtime.getRuntime().availableProcessors());

ExecutorService threadPool = new ThreadPoolExecutor(

2,

5,

1,

TimeUnit.SECONDS,

new LinkedBlockingQueue<>(3),

Executors.defaultThreadFactory(),

new ThreadPoolExecutor.AbortPolicy()

);

// 最大承载:Queue + max、超过报异常

try {

for (int i = 1; i <= 9; i++) {

threadPool.execute(() -> {

System.out.println(Thread.currentThread().getName() + "ok");

});

}

} finally {

threadPool.shutdown();

}

}

}


https://www.xamrdz.com/backend/3fx1945777.html

相关文章: