日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

初學(xué)者java入門基礎(chǔ)知識 java雙端隊(duì)列原理( 二 )

2)LinkedBlockingQueueLinkedBlockingQueue 是一個由鏈表實(shí)現(xiàn)的有界阻塞隊(duì)列 , 容量默認(rèn)值為 Integer.MAX_VALUE , 也可以自定義容量 , 建議指定容量大小 , 默認(rèn)大小在添加速度大于刪除速度情況下有造成內(nèi)存溢出的風(fēng)險(xiǎn) , LinkedBlockingQueue 是先進(jìn)先出的方式存儲元素 。
3)ArrayBlockingQueueArrayBlockingQueue 是一個有邊界的阻塞隊(duì)列 , 它的內(nèi)部實(shí)現(xiàn)是一個數(shù)組 。它的容量是有限的 , 我們必須在其初始化的時(shí)候指定它的容量大小 , 容量大小一旦指定就不可改變 。ArrayBlockingQueue 也是先進(jìn)先出的方式存儲數(shù)據(jù) , ArrayBlockingQueue 內(nèi)部的阻塞隊(duì)列是通過重入鎖 ReenterLock 和 Condition 條件隊(duì)列實(shí)現(xiàn)的 , 因此 ArrayBlockingQueue 中的元素存在公平訪問與非公平訪問的區(qū)別 , 對于公平訪問隊(duì)列 , 被阻塞的線程可以按照阻塞的先后順序訪問隊(duì)列 , 即先阻塞的線程先訪問隊(duì)列 。而非公平隊(duì)列 , 當(dāng)隊(duì)列可用時(shí) , 阻塞的線程將進(jìn)入爭奪訪問資源的競爭中 , 也就是說誰先搶到誰就執(zhí)行 , 沒有固定的先后順序 。示例代碼如下:
【初學(xué)者java入門基礎(chǔ)知識 java雙端隊(duì)列原理】// 默認(rèn)非公平阻塞隊(duì)列ArrayBlockingQueue queue = new ArrayBlockingQueue(6);// 公平阻塞隊(duì)列ArrayBlockingQueue queue2 = new ArrayBlockingQueue(6,true);// ArrayBlockingQueue 源碼展示public ArrayBlockingQueue(int capacity) {this(capacity, false);}public ArrayBlockingQueue(int capacity, boolean fair) {if (capacity <= 0)throw new IllegalArgumentException();this.items = new Object[capacity];lock = new ReentrantLock(fair);notEmpty = lock.newCondition();notFull =lock.newCondition();}4)DelayQueueDelayQueue 是一個支持延時(shí)獲取元素的無界阻塞隊(duì)列 , 隊(duì)列中的元素必須實(shí)現(xiàn) Delayed 接口 , 在創(chuàng)建元素時(shí)可以指定延遲時(shí)間 , 只有到達(dá)了延遲的時(shí)間之后 , 才能獲取到該元素 。實(shí)現(xiàn)了 Delayed 接口必須重寫兩個方法  , getDelay(TimeUnit) 和 compareTo(Delayed) , 如下代碼所示:
class DelayElement implements Delayed {@Override// 獲取剩余時(shí)間public long getDelay(TimeUnit unit) {// do something}@Override// 隊(duì)列里元素的排序依據(jù)public int compareTo(Delayed o) {// do something}}DelayQueue 使用的完整示例 , 請參考以下代碼:
public class DelayTest {public static void main(String[] args) throws InterruptedException {DelayQueue delayQueue = new DelayQueue();delayQueue.put(new DelayElement(1000));delayQueue.put(new DelayElement(3000));delayQueue.put(new DelayElement(5000));System.out.println("開始時(shí)間:" +DateFormat.getDateTimeInstance().format(new Date()));while (!delayQueue.isEmpty()){System.out.println(delayQueue.take());}System.out.println("結(jié)束時(shí)間:" +DateFormat.getDateTimeInstance().format(new Date()));} static class DelayElement implements Delayed {// 延遲截止時(shí)間(單面:毫秒)long delayTime = System.currentTimeMillis(); public DelayElement(long delayTime) {this.delayTime = (this.delayTime + delayTime); } @Override// 獲取剩余時(shí)間 public long getDelay(TimeUnit unit) {return unit.convert(delayTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS); } @Override// 隊(duì)列里元素的排序依據(jù) public int compareTo(Delayed o) {if (this.getDelay(TimeUnit.MILLISECONDS) > o.getDelay(TimeUnit.MILLISECONDS)) {return 1;} else if (this.getDelay(TimeUnit.MILLISECONDS) < o.getDelay(TimeUnit.MILLISECONDS)) {return -1;} else {return 0;}}@Overridepublic String toString() {return DateFormat.getDateTimeInstance().format(new Date(delayTime)); } }}程序執(zhí)行結(jié)果:

開始時(shí)間:2019-6-13 20:40:38 2019-6-13 20:40:39 2019-6-13 20:40:41 2019-6-13 20:40:43 結(jié)束時(shí)間:2019-6-13 20:40:43

推薦閱讀