欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中的ConcurrentLinkedQueue使用解析

 更新時(shí)間:2023年12月28日 09:30:30   作者:小宇宙121  
這篇文章主要介紹了Java中的ConcurrentLinkedQueue使用解析,一個(gè)基于鏈接節(jié)點(diǎn)的無界線程安全隊(duì)列,此隊(duì)列按照 FIFO(先進(jìn)先出)原則對(duì)元素進(jìn)行排序,隊(duì)列的頭部是隊(duì)列中時(shí)間最長(zhǎng)的元素,需要的朋友可以參考下

定義

一個(gè)基于鏈接節(jié)點(diǎn)的無界線程安全隊(duì)列。此隊(duì)列按照 FIFO(先進(jìn)先出)原則對(duì)元素進(jìn)行排序。

隊(duì)列的頭部是隊(duì)列中時(shí)間最長(zhǎng)的元素。

隊(duì)列的尾部是隊(duì)列中時(shí)間最短的元素。 新的元素插入到隊(duì)列的尾部,隊(duì)列獲取操作從隊(duì)列頭部獲得元素。

當(dāng)多個(gè)線程共享訪問一個(gè)公共 collection 時(shí),ConcurrentLinkedQueue 是一個(gè)恰當(dāng)?shù)倪x擇。

此隊(duì)列不允許使用 null 元素。

函數(shù)

  • offer(E e) 將指定元素插入此隊(duì)列的尾部。
  • poll() 獲取并移除此隊(duì)列的頭,如果此隊(duì)列為空,則返回 null
  • add() 與offer(E e)完全一樣,都是往隊(duì)列尾部添加元素
  • peek() 獲取但不移除此隊(duì)列的頭;如果此隊(duì)列為空,則返回 null
  • remove(Object o) 從隊(duì)列中移除指定元素的單個(gè)實(shí)例(如果存在)
  • contains(Object o) 如果此隊(duì)列包含指定元素,則返回 true
  • toArray() 返回以恰當(dāng)順序包含此隊(duì)列所有元素的數(shù)組
  • toArray(T[] a) 返回以恰當(dāng)順序包含此隊(duì)列所有元素的數(shù)組;返回?cái)?shù)組的運(yùn)行時(shí)類型是指定數(shù)組的運(yùn)行時(shí)類型
  • size() 返回此隊(duì)列中的元素?cái)?shù)量
  • isEmpty 返回隊(duì)列是否為空

注意:當(dāng)需要判斷隊(duì)列是否有值時(shí),推薦使用isEmpty(),size()的時(shí)間復(fù)雜度是o(n),當(dāng)數(shù)據(jù)量很大時(shí),會(huì)比isEmpty()消耗明顯多的時(shí)間。

下面比較一下isEmpty()和size()耗時(shí):

public class ConcurrentLinkedQueueTest {
    private static final Logger logger = LoggerFactory.getLogger(ConcurrentLinkedQueueTest.class);
    public static void main(String args[]) throws InterruptedException {
        ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
        int serviceNum = 10;//服務(wù)窗口數(shù)
        int perpeoNum = 10000;//買車票人數(shù)
        CountDownLatch countDownLatch = new CountDownLatch(serviceNum);//初始值是線程的個(gè)數(shù)
        //將買票人數(shù)放入隊(duì)列
        for(int i=0; i<perpeoNum; i++){
            queue.add("買票人_" + i);
        }
        //執(zhí)行10個(gè)線程從隊(duì)列取出買票人
        logger.info("-----開始賣票了--------");
        long start = System.currentTimeMillis();
        ExecutorService service = Executors.newFixedThreadPool(10);
        for(int i=0; i<serviceNum; i++){
            service.submit(new Windows("窗口號(hào)_" + i , queue, countDownLatch));
        }
        //等待所有線程都執(zhí)行完,主線程再執(zhí)行。
        countDownLatch.await();
		logger.info("-----所有人都買完票了----");
        long time = System.currentTimeMillis() - start;
        logger.info("總消耗時(shí)間是:{}",time);
        service.shutdown();
    }
    private static class Windows implements Runnable{
        private String name;
        private ConcurrentLinkedQueue queue;
        private CountDownLatch countDownLatch;
        public Windows(String name, ConcurrentLinkedQueue queue, CountDownLatch countDownLatch){
            this.name = name;
            this.queue = queue;
            this.countDownLatch = countDownLatch;
        }
        @Override
        public void run() {
        //   while (!queue.isEmpty()){
            while(queue.size() > 0){  
                logger.info(queue.poll()  + "---買票完畢---{}", name );
            }
            countDownLatch.countDown();//線程執(zhí)行完,count值-1
        }
    }
}

執(zhí)行結(jié)果: 使用size(),耗時(shí)511ms; 使用isEmpty(),耗時(shí)125ms; 所以優(yōu)先使用isEmpty();

CountDownLatch

CountDownLatch是一個(gè)非常實(shí)用的多線程控制工具類,例如,應(yīng)用程序的主線程希望在負(fù)責(zé)啟動(dòng)框架服務(wù)的線程已經(jīng)啟動(dòng)所有的框架服務(wù)之后再執(zhí)行。

CountDownLatch是通過一個(gè)計(jì)數(shù)器來實(shí)現(xiàn)的,計(jì)數(shù)器的初始值為線程的數(shù)量。

每當(dāng)一個(gè)線程完成了自己的任務(wù)后,計(jì)數(shù)器的值就會(huì)減1。

當(dāng)計(jì)數(shù)器值到達(dá)0時(shí),它表示所有的線程已經(jīng)完成了任務(wù),然后在閉鎖上等待的線程就可以恢復(fù)執(zhí)行任務(wù)。

到此這篇關(guān)于Java中的ConcurrentLinkedQueue使用解析的文章就介紹到這了,更多相關(guān)ConcurrentLinkedQueue使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論