Executors建立執行緒池

2021-09-20 09:38:05 字數 411 閱讀 2179

兩個靜態方法:

executorservice exec = executors.newcachethreadpool(); 這個方式會為每個任務建立乙個執行緒。

executorservice exec = executors.newfixedthreadpool(10); 建立10個執行緒的執行緒池。

執行某個實現runable介面的類testthread:

exec.execute(new testthread)。

執行完關閉:exec.shutdown()。

cachethreadpool在程式執行過程中通常會建立與所需數量相同的執行緒,然後在它**舊執行緒時停止建立新執行緒,因此它是合理的executor的首選。只有當這種方式引發問題時,你才需要切換到fixedthreadpool。

executors建立執行緒池

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

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

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

Executors 構建執行緒池

executors包含一系列靜態方法,可以用於構建執行緒池。返回實現了 executorservice 介面的物件 返回實現了scheduledexecutorservice介面的物件 當用完乙個執行緒池時,要呼叫shutdown或shutdownnow。總結使用執行緒池需要做的事情 呼叫execu...