ArrayBlockingQueue 原始碼分析學習

2021-10-24 02:12:02 字數 1617 閱讀 5897

arrayblockingqueue 是乙個基於陣列的阻塞佇列,是乙個有界佇列,有界也就意味著,它不能夠儲存無限多數量的物件。所以在建立 arrayblockingqueue 時,必須要給它指定乙個佇列的大小。

底層維護的是object陣列。主要方法有:

take: 當取出元素時:

add: 當新增元素時,如果佇列已滿則會異常

poll: 當取出元素時,如果隊列為空則返回 null

reentrantlock lock unlock 原理分析

condition await signal 阻塞和喚醒 原理分析

arrayblockingqueue 主要思想的**

// 儲存資料

final object[

] items;

// 下乙個take,poll的索引值

int takeindex;

// 下乙個put、offer、add 的索引值

int putindex;

// 儲存當前的數量

int count;

/** main lock guarding all access */

final reentrantlock lock;

/** condition for waiting takes */

private

final condition notempty;

/** condition for waiting puts */

private

final condition notfull;

// 非公平重入鎖

public

arrayblockingqueue

(int capacity)

public

arrayblockingqueue

(int capacity,

boolean fair)

// put 新增乙個元素到佇列中,如果當前佇列已滿,則阻塞等待

public

void

put(e e)

throws interruptedexception

finally

}// 新增元素到佇列中

private

void

enqueue

(e x)

// 獲取乙個元素,如果該隊列為空,則進行阻塞

public e take()

throws interruptedexception

finally

}// 取出佇列

private e dequeue()

// 當新增乙個元素時,如果佇列已滿會異常,未滿則返回true

public

boolean

add(e e)

// 如果佇列已滿,返回false ,天極愛成功返回true

public

boolean

offer

(e e)

}finally

}// 獲取元素,如果隊列為空,則返回 null

public e poll()

finally

}

Redux createStore原始碼學習

redux apiexport原始碼結構上面我們看到了redux的api和原始碼結構,看的出來,warning.js和index.js不用解析,都看得懂,關鍵時其餘的幾個module,那我們從最重要的createstore講起。export var actiontypes 首先定義了乙個action...

pytorch geometric 原始碼學習

作者大神真的太屌了,膜拜,工程實現能力太強了 本文希望能夠記錄學習其原始碼的過程 data dataset 部分 涉及優化?property 一種python內建裝飾器,可以將乙個成員函式當成成員變數來訪問,例如 class planetoid inmemorydataset url def ini...

spring原始碼分析 spring原始碼分析

1.spring 執行原理 spring 啟動時讀取應用程式提供的 bean 配置資訊,並在 spring 容器中生成乙份相應的 bean 配置登錄檔,然後根據這張登錄檔例項化 bean,裝配好 bean 之間的依賴關係,為上 層應用提供準備就緒的執行環境。二 spring 原始碼分析 1.1spr...