這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)深入淺析java并發(fā)中的ArrayBlockingQueue,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
java并發(fā)之ArrayBlockingQueue詳細(xì)介紹
ArrayBlockingQueue是常用的線程集合,在線程池中也常常被當(dāng)做任務(wù)隊(duì)列來(lái)使用。使用頻率特別高。他是維護(hù)的是一個(gè)循環(huán)隊(duì)列(基于數(shù)組實(shí)現(xiàn)),循環(huán)結(jié)構(gòu)在數(shù)據(jù)結(jié)構(gòu)中比較常見(jiàn),但是在源碼實(shí)現(xiàn)中還是比較少見(jiàn)的。
線程安全的實(shí)現(xiàn)
線程安全隊(duì)列,基本是離不開鎖的。ArrayBlockingQueue使用的是ReentrantLock,配合兩種Condition,實(shí)現(xiàn)了集合的線程安全操作。這里稍微說(shuō)一個(gè)好習(xí)慣,下面是成員變量的聲明。
private static final long serialVersionUID = -817911632652898426L; final Object[] items; int takeIndex; int putIndex; int count; final ReentrantLock lock; private final Condition notEmpty; private final Condition notFull; transient Itrs itrs = null;