BlockingQueue實現(xiàn)生產者消費者模式

  • 提供put方法和take方法自動實現(xiàn)等待

put方法源碼

  • 如果空間不足夠的時候就會等待
 /**
     * Inserts the specified element into this queue, waiting if necessary
     * for space to become available.
     *
     * @param e the element to add
     * @throws InterruptedException if interrupted while waiting
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this queue
     * @throws NullPointerException if the specified element is null
     * @throws IllegalArgumentException if some property of the specified
     *         element prevents it from being added to this queue
     */
    void put(E e) throws InterruptedException;

take方法源碼

  • 移走隊頭元素椎例,隊空時等待
/**
     * Retrieves and removes the head of this queue, waiting if necessary
     * until an element becomes available.
     *
     * @return the head of this queue
     * @throws InterruptedException if interrupted while waiting
     */
package thread;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ProducerConsumer3 {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(1);
        ProducerConsumer3.Producer p1 = new ProducerConsumer3.Producer(queue);
        ProducerConsumer3.Consumer c1 = new ProducerConsumer3.Consumer(queue);
        p1.start();
        c1.start();
        p1.join();
        c1.join();
    }

    static class Producer extends Thread {
        BlockingQueue<Integer> queue;

        Producer(BlockingQueue<Integer> queue) {
            this.queue = queue;
        }

        @Override
        public void run() {
            while (true) {
                int tmp = new Random().nextInt();
                try {
                    queue.put(tmp);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Producing " + tmp);
            }

        }

    }

    static class Consumer extends Thread {
        BlockingQueue<Integer> queue;

        Consumer(BlockingQueue<Integer> queue) {
            this.queue = queue;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    System.out.println("Consuming " + queue.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市雏门,隨后出現(xiàn)的幾起案子屁商,更是在濱河造成了極大的恐慌良姆,老刑警劉巖鹅很,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件像吻,死亡現(xiàn)場離奇詭異堕汞,居然都是意外死亡勺爱,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進店門讯检,熙熙樓的掌柜王于貴愁眉苦臉地迎上來琐鲁,“玉大人,你說我怎么就攤上這事人灼∥Ф危” “怎么了?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵投放,是天一觀的道長奈泪。 經常有香客問我,道長灸芳,這世上最難降的妖魔是什么涝桅? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮烙样,結果婚禮上冯遂,老公的妹妹穿的比我還像新娘。我一直安慰自己谒获,他們只是感情好蛤肌,可當我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著批狱,像睡著了一般裸准。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上精耐,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天狼速,我揣著相機與錄音琅锻,去河邊找鬼卦停。 笑死,一個胖子當著我的面吹牛恼蓬,可吹牛的內容都是我干的惊完。 我是一名探鬼主播,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼处硬,長吁一口氣:“原來是場噩夢啊……” “哼小槐!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤凿跳,失蹤者是張志新(化名)和其女友劉穎件豌,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體控嗜,經...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡茧彤,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了疆栏。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片曾掂。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖壁顶,靈堂內的尸體忽然破棺而出珠洗,到底是詐尸還是另有隱情,我是刑警寧澤若专,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布许蓖,位于F島的核電站,受9級特大地震影響富岳,放射性物質發(fā)生泄漏蛔糯。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一窖式、第九天 我趴在偏房一處隱蔽的房頂上張望蚁飒。 院中可真熱鬧,春花似錦萝喘、人聲如沸淮逻。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽爬早。三九已至,卻和暖如春启妹,著一層夾襖步出監(jiān)牢的瞬間筛严,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工饶米, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留桨啃,地道東北人。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓檬输,卻偏偏與公主長得像照瘾,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子丧慈,可洞房花燭夜當晚...
    茶點故事閱讀 44,960評論 2 355