Executors 構建執行緒池

2022-09-03 05:42:07 字數 335 閱讀 6061

executors包含一系列靜態方法,可以用於構建執行緒池。

返回實現了 executorservice 介面的物件:

返回實現了scheduledexecutorservice介面的物件

當用完乙個執行緒池時,要呼叫shutdown或shutdownnow。

總結使用執行緒池需要做的事情:

呼叫executor的類方法newcachedthreadpool等;

呼叫submit提交runnable或callable物件;

如果想取消任務,或者提交的是callable物件,要儲存好future物件;

當不再提交任務時,呼叫shutdown方法。

executors建立執行緒池

private static final executor exec executors.newfixedthreadpool 50 runnable runnable new runnable exec.execute runnable callablecallable new callable ...

Executors建立執行緒池

兩個靜態方法 executorservice exec executors.newcachethreadpool 這個方式會為每個任務建立乙個執行緒。executorservice exec executors.newfixedthreadpool 10 建立10個執行緒的執行緒池。執行某個實現ru...

執行緒池 Executors類建立執行緒池

executors靜態工廠建立幾種常用執行緒池 1.建立了乙個固定執行緒數量的執行緒池,可以控制線程最大併發數,超出的執行緒會在佇列中等待。newfixedthreadpool int nthreads 執行緒池中線程數量是要指定傳入的,注意在固定大小的執行緒池中使用的阻塞佇列是linkedbloc...