多執行緒程式設計(三)

2021-09-12 07:24:04 字數 3261 閱讀 1889

1、執行緒池:

- 防止大量執行非同步任務時,頻繁的建立和銷毀執行緒物件帶來的系統開銷;

- 根據執行緒池型別的不同可以有效防止系統中的執行緒資源消耗過多;

2、執行緒池核心引數:

- corepoolsize:執行緒池核心執行緒個數(包括空閒執行緒)

- maximunpoolsize:執行緒池中線程的最大數量

- workqueue:存放execute方法提交的任務的阻塞佇列(blockingqueue),如:

synchronousblockingqueue、arrayblockingqueue、linkedblockingqueue等。

- threadfactory:建立執行緒的工廠

- keepalivetime:執行緒池中線程數量超過corepoolsize時,池中空閒執行緒的存活時間

- unit:keepalivetime的單位

- rejectexecutionhandler:任務拒絕策略

3、類結構:

4、常見執行緒池的種類:

- newsinglethreadexecutor()

- newfixedthreadpool(n)

- newcachedthreadpool()

- newscheduledthreadpool()

注:以上幾種執行緒池型別均是由於設定的corepoolsize、maximunpoolsize、workqueue型別

以及rejectexecutionhandler產生的結果。

5、執行緒池的狀態(volatile int runstate):

static final int running = 0;//執行狀態

static final int shutdown = 1;//顯式執行shutdown()方法後的狀態,仍會執行完所有

workqueue中的所有任務

static final int stop = 2;//顯式呼叫shutdownnow()方法後的狀態,workqueue中的任

務不再執行且嘗試終止正在執行的任務

static final int terminated = 3;//所有任務執行完畢,執行緒池中的全部工作執行緒銷毀完畢

6、執行緒池的核心方法:

execute()

submit()

shutdown()

shutdownnow()

7、任務快取策略:

- 立即提交策略:使用synchronousblockingqueue工作佇列,execute方法執行提交的任務不

會快取提交的任務,直接在建立執行緒執行,要求maximumpoolsize足夠大。

- 無界佇列策略:使用linkedblockingqueue工作佇列,任務被提交時如果執行緒池中的執行緒數

小於corepoolsize時,直接建立執行緒執行;如果執行緒數 >= corepoolsize時,

優先向工作佇列中新增,等待工作執行緒執行,如:newfixedthreadpool

- 有界佇列策略:使用arrayblockingqueue工作佇列,當任務佇列已滿且執行緒數已達到最大值

選擇丟棄任務。

8、任務拒絕策略:

- threadpoolexecutor.abortpolicy:丟棄任務並丟擲rejectedexecutionexception異常

- threadpoolexecutor.discardpolicy:也是丟棄任務,但是不丟擲異常。

- threadpoolexecutor.discardoldestpolicy:丟棄佇列最前面的任務,然後重新嘗試執行任務(重複此過程)

- threadpoolexecutor.callerrunspolicy:由呼叫執行緒處理該任務

9、測試**:

(1)newsinglethreadexecutor:

public class threadpooltest

static class mythread implements runnable catch (interruptedexception e)

system.out.println(thread.currentthread().getname());}}

}執行結果:

執行緒池狀態改變

pool-1-thread-1

pool-1-thread-1

pool-1-thread-1

可以看到所有的任務是由同乙個執行緒執行的。

(2)newfixedthreadpool():

public class threadpooltest

static class mythread implements runnable catch (interruptedexception e)

system.out.println(thread.currentthread().getname());}}

}執行結果:

1 23 3

3 執行緒池狀態改變

pool-1-thread-1

pool-1-thread-2

pool-1-thread-3

pool-1-thread-1

pool-1-thread-2

可以看到執行緒池中的執行緒數最多保持在3個。

(3)newcachedthreadpool():

public class threadpooltest

static class mythread implements runnable catch (interruptedexception e)

system.out.println(thread.currentthread().getname());}}

}執行結果:

1 23 4

5 執行緒池狀態改變

pool-1-thread-1

pool-1-thread-2

pool-1-thread-3

pool-1-thread-4

pool-1-thread-5

可以從結果看到每乙個任務都是由不同的執行緒執行的。

多執行緒程式設計 三

放入資料 獲取資料 arrayblockingqueu 和 linkedblockingqueue arrayblockingqueu 是用陣列實現的有界阻塞佇列,並按照先進先出 fifo 的原則對元素進行排序。第乙個引數是容量 第二個引數是是否要保證執行緒的公平地訪問佇列 預設是false arr...

多執行緒程式設計(三)

條件變數 mutex my mutex condition variable my cond unique lockul my mutex mycond.wait ul,lambda表示式或者可呼叫物件 條件變數需要繫結到unique lock上,wait的作用是,如果第二個引數的返回值是true,...

Linux 多執行緒程式設計(三)

繼續昨天的執行緒同步,條件變數 condition variables 是用於執行緒間,通訊共享資料狀態改變的機制。簡介條件變數的建立和銷毀 等待條件變數 喚醒等待條件變數的執行緒 簡介 當執行緒互斥地訪問一些共享的狀態時,往往會有些執行緒需要等到這些狀態改變後才應該繼續執行。如 有乙個共享的佇列,...