摘要:一定義就是生產(chǎn)者消費(fèi)者模式。消費(fèi)者參與者從參與者獲取數(shù)據(jù),進(jìn)行處理。通道參與者從參與者處接受參與者,并保管起來(lái),并應(yīng)參與者的要求,將參與者傳送出去。為確保安全性,參與者與參與者要對(duì)訪(fǎng)問(wèn)共享互斥。
一、定義
Producer-Consumer Pattern就是生產(chǎn)者-消費(fèi)者模式。
生產(chǎn)者和消費(fèi)者在為不同的處理線(xiàn)程,生產(chǎn)者必須將數(shù)據(jù)安全地交給消費(fèi)者,消費(fèi)者進(jìn)行消費(fèi)時(shí),如果生產(chǎn)者還沒(méi)有建立數(shù)據(jù),則消費(fèi)者需要等待。
一般來(lái)說(shuō),可能存在多個(gè)生產(chǎn)者和消費(fèi)者,不過(guò)也有可能生產(chǎn)者和消費(fèi)者都只有一個(gè),當(dāng)雙方都只有一個(gè)時(shí),我們也稱(chēng)之為Pipe Pattern。
該案例中,定義了3個(gè)角色:廚師、客人、桌子。
廚師(生產(chǎn)者)定義:
public class MakerThread extends Thread { private final Random random; private final Table table; private static int id = 0; //蛋糕的流水號(hào)(所有廚師共通) public MakerThread(String name, Table table, long seed) { super(name); this.table = table; this.random = new Random(seed); } public void run() { try { while (true) { Thread.sleep(random.nextInt(1000)); String cake = "[ Cake No." + nextId() + " by " + getName() + " ]"; table.put(cake); } } catch (InterruptedException e) { } } private static synchronized int nextId() { return id++; } }
客人(消費(fèi)者)定義:
public class EaterThread extends Thread { private final Random random; private final Table table; public EaterThread(String name, Table table, long seed) { super(name); this.table = table; this.random = new Random(seed); } public void run() { try { while (true) { String cake = table.take(); Thread.sleep(random.nextInt(1000)); } } catch (InterruptedException e) { } } }
桌子(隊(duì)列)定義:
public class Table { private final String[] buffer; private int tail; private int head; private int count; ? public Table(int count) { this.buffer = new String[count]; this.head = 0; this.tail = 0; this.count = 0; } public synchronized void put(String cake) throws InterruptedException { System.out.println(Thread.currentThread().getName() + " puts " + cake); while (count >= buffer.length) { wait(); } buffer[tail] = cake; tail = (tail + 1) % buffer.length; count++; notifyAll(); } public synchronized String take() throws InterruptedException { while (count <= 0) { wait(); } String cake = buffer[head]; head = (head + 1) % buffer.length; count--; notifyAll(); System.out.println(Thread.currentThread().getName() + " takes " + cake); return cake; } }
執(zhí)行:
public class Main { public static void main(String[] args) { Table table = new Table(3); new MakerThread("MakerThread-1", table, 31415).start(); new MakerThread("MakerThread-2", table, 92653).start(); new MakerThread("MakerThread-3", table, 58979).start(); new EaterThread("EaterThread-1", table, 32384).start(); new EaterThread("EaterThread-2", table, 62643).start(); new EaterThread("EaterThread-3", table, 38327).start(); } }三、模式講解
Producer-Consumer模式的角色如下:
Data(數(shù)據(jù))參與者
Data代表了實(shí)際生產(chǎn)或消費(fèi)的數(shù)據(jù)。
Producer(生產(chǎn)者)參與者
Producer會(huì)創(chuàng)建Data,然后傳遞給Channel參與者。
Consumer(消費(fèi)者)參與者
Consumer從Channel參與者獲取Data數(shù)據(jù),進(jìn)行處理。
Channel(通道)參與者
Channel從Producer參與者處接受Data參與者,并保管起來(lái),并應(yīng)Consumer參與者的要求,將Data參與者傳送出去。為確保安全性,Producer參與者與Consumer參與者要對(duì)訪(fǎng)問(wèn)共享互斥。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://hztianpu.com/yun/71514.html
摘要:和方法會(huì)一直阻塞調(diào)用線(xiàn)程,直到線(xiàn)程被中斷或隊(duì)列狀態(tài)可用和方法會(huì)限時(shí)阻塞調(diào)用線(xiàn)程,直到超時(shí)或線(xiàn)程被中斷或隊(duì)列狀態(tài)可用。 showImg(https://segmentfault.com/img/bVbgyPy?w=1191&h=670); 本文首發(fā)于一世流云專(zhuān)欄:https://segmentfault.com/blog... 一、引言 從本節(jié)開(kāi)始,我們將介紹juc-collectio...
摘要:個(gè)巡個(gè)推系統(tǒng)監(jiān)控隨著個(gè)推業(yè)務(wù)的不斷擴(kuò)展,用戶(hù)量不斷的增加,個(gè)推急需一套完整的監(jiān)控系統(tǒng)來(lái)實(shí)時(shí)保證系統(tǒng)和業(yè)務(wù)的正常運(yùn)轉(zhuǎn)。系統(tǒng)難點(diǎn)與設(shè)計(jì)多元化的數(shù)據(jù)基于推送業(yè)務(wù),個(gè)推擴(kuò)展出許多獨(dú)立運(yùn)行的系統(tǒng),而且每個(gè)系統(tǒng)的監(jiān)控?cái)?shù)據(jù)也不一樣。 什么是系統(tǒng)監(jiān)控對(duì)于功能簡(jiǎn)單,用戶(hù)量較少的軟件系統(tǒng),大部分公司不需要額外的監(jiān)控系統(tǒng)來(lái)保證公司業(yè)務(wù)的正常運(yùn)行。而當(dāng)公司發(fā)展到一定程度,系統(tǒng)越來(lái)越多元化,單一系統(tǒng)也越來(lái)越復(fù)雜...
摘要:寄語(yǔ)天眼之父南仁東,心無(wú)旁騖,為崇山峻嶺間的中國(guó)天眼燃盡生命,看似一口大鍋,天眼是世界上最大最靈敏的單口徑射電望遠(yuǎn)鏡,可以接受百億光年外的電磁信號(hào)。南仁東總工程師執(zhí)著追求科學(xué)夢(mèng)想的精神,將激勵(lì)一代又一代科技工作者繼續(xù)奮斗,勇攀世界科技高峰。 歡迎進(jìn)入JAVA基礎(chǔ)課程 博客地址:https://segmentfault.com/a/11...本系列文章將主要針對(duì)JAVA一些基礎(chǔ)知識(shí)點(diǎn)進(jìn)行...
摘要:寄語(yǔ)天眼之父南仁東,心無(wú)旁騖,為崇山峻嶺間的中國(guó)天眼燃盡生命,看似一口大鍋,天眼是世界上最大最靈敏的單口徑射電望遠(yuǎn)鏡,可以接受百億光年外的電磁信號(hào)。南仁東總工程師執(zhí)著追求科學(xué)夢(mèng)想的精神,將激勵(lì)一代又一代科技工作者繼續(xù)奮斗,勇攀世界科技高峰。 歡迎進(jìn)入JAVA基礎(chǔ)課程 博客地址:https://segmentfault.com/a/11...本系列文章將主要針對(duì)JAVA一些基礎(chǔ)知識(shí)點(diǎn)進(jìn)行...
摘要:哪吒社區(qū)技能樹(shù)打卡打卡貼函數(shù)式接口簡(jiǎn)介領(lǐng)域優(yōu)質(zhì)創(chuàng)作者哪吒公眾號(hào)作者架構(gòu)師奮斗者掃描主頁(yè)左側(cè)二維碼,加入群聊,一起學(xué)習(xí)一起進(jìn)步歡迎點(diǎn)贊收藏留言前情提要無(wú)意間聽(tīng)到領(lǐng)導(dǎo)們的談話(huà),現(xiàn)在公司的現(xiàn)狀是碼農(nóng)太多,但能獨(dú)立帶隊(duì)的人太少,簡(jiǎn)而言之,不缺干 ? 哪吒社區(qū)Java技能樹(shù)打卡?【打卡貼 day2...
閱讀 1184·2021-09-29 09:35
閱讀 4929·2021-09-22 15:24
閱讀 1595·2021-07-25 21:37
閱讀 2295·2019-08-30 14:17
閱讀 1069·2019-08-30 13:56
閱讀 2491·2019-08-29 17:07
閱讀 1618·2019-08-29 12:44
閱讀 2822·2019-08-26 18:26