日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

多生产者多消费者问题

發布時間:2025/3/20 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多生产者多消费者问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*** 多生產者 多消費者問題* * 該代碼存在死鎖問題* 因為有可能喚醒本方 * @author 黃二狗**/public class Test {public static void main(String[] args) {Resource r = new Resource();Producer producer = new Producer(r);Consumer consumer = new Consumer(r);Thread t0 = new Thread(producer);Thread t1 = new Thread(producer);Thread t2 = new Thread(consumer);Thread t3 = new Thread(consumer);t0.start();t1.start();t2.start();t3.start();} }class Resource {private String name;private int count;private boolean flag;public synchronized void set(String name) throws InterruptedException {while(flag) this.wait(); // t0 t1 this.name = name + count;count++; System.out.println(Thread.currentThread().getName() + "....生產者..." + this.name);flag = true;this.notify();}public synchronized void out() throws InterruptedException {while(!flag) //t2 t3this.wait(); System.out.println(Thread.currentThread().getName() + "....消費者..." + this.name);flag = false;this.notify();} }class Producer implements Runnable {private Resource r;public Producer(Resource r) {this.r = r;}@Overridepublic void run() {while(true) {try {r.set("烤鴨");} catch (InterruptedException e) {e.printStackTrace();}} } }class Consumer implements Runnable {private Resource r;public Consumer(Resource r) {this.r = r;}@Overridepublic void run() {try {while(true) {r.out();}} catch (InterruptedException e) {// TODO Auto-generated catch block e.printStackTrace();}} }

?

改進方法:將notify()換成notifyAll(),這樣的話就不會產生死鎖了.

?

package test;/*** 多生產者 多消費者問題* * 該代碼存在死鎖問題* 因為有可能喚醒本方 * @author 黃二狗**/public class Test {public static void main(String[] args) {Resource r = new Resource();Producer producer = new Producer(r);Consumer consumer = new Consumer(r);Thread t0 = new Thread(producer);Thread t1 = new Thread(producer);Thread t2 = new Thread(consumer);Thread t3 = new Thread(consumer);t0.start();t1.start();t2.start();t3.start();} }class Resource {private String name;private int count;private boolean flag;public synchronized void set(String name) throws InterruptedException {while(flag) this.wait(); // t0 t1 this.name = name + count;count++; System.out.println(Thread.currentThread().getName() + "....生產者..." + this.name);flag = true;this.notifyAll();}public synchronized void out() throws InterruptedException {while(!flag) //t2 t3this.wait(); System.out.println(Thread.currentThread().getName() + "....消費者..." + this.name);flag = false;this.notifyAll();} }class Producer implements Runnable {private Resource r;public Producer(Resource r) {this.r = r;}@Overridepublic void run() {while(true) {try {r.set("烤鴨");} catch (InterruptedException e) {e.printStackTrace();}} } }class Consumer implements Runnable {private Resource r;public Consumer(Resource r) {this.r = r;}@Overridepublic void run() {try {while(true) {r.out();}} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }

  

?

package test;import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;/*** 使用Lock和Condition解決喚醒的低效率問題**/public class Test {public static void main(String[] args) {Resource r = new Resource();Producer producer = new Producer(r);Consumer consumer = new Consumer(r);Thread t0 = new Thread(producer);Thread t1 = new Thread(producer);Thread t2 = new Thread(consumer);Thread t3 = new Thread(consumer);t0.start();t1.start();t2.start();t3.start();} }class Resource {private String name;private int count;private boolean flag;private Lock lock = new ReentrantLock();private Condition producer_condition = lock.newCondition();private Condition consumer_condition = lock.newCondition();public void set(String name) throws InterruptedException {lock.lock();while(flag) producer_condition.await();this.name = name + count;count++; System.out.println(Thread.currentThread().getName() + "....生產者..." + this.name);flag = true;consumer_condition.signal();lock.unlock();}public void out() throws InterruptedException {lock.lock();while(!flag) consumer_condition.await();System.out.println(Thread.currentThread().getName() + "....消費者..." + this.name);flag = false;producer_condition.signal();lock.unlock();} }class Producer implements Runnable {private Resource r;public Producer(Resource r) {this.r = r;}@Overridepublic void run() {while(true) {try {r.set("烤鴨");} catch (InterruptedException e) {e.printStackTrace();}} } }class Consumer implements Runnable {private Resource r;public Consumer(Resource r) {this.r = r;}@Overridepublic void run() {try {while(true) {r.out();}} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }

  

package test;import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;/*** 真實場景下的多生產者多消費者模型* @author 黃二狗**/ public class BoundeBuffer {final Lock lock = new ReentrantLock();final Condition notFull = lock.newCondition();final Condition notEmpty = lock.newCondition();final Object [] items = new Object[100];int putPosition = 0;int takePosition = 0;int count = 0;public void put(Object obj) throws InterruptedException {lock.lock();while(items.length == count) {notFull.await();}items[putPosition] = obj;putPosition = putPosition + 1;if(putPosition == items.length)putPosition = 0;count = count + 1;notEmpty.signal();lock.unlock();}public Object take() throws InterruptedException {lock.lock();try {while(count == 0) {notEmpty.wait();}Object x = items[takePosition];takePosition = takePosition + 1;if(takePosition == items.length) {takePosition ++;}count = count - 1;notFull.signal();return x ;} finally {lock.unlock(); }}public static void main(String[] args) {} }

?

轉載于:https://www.cnblogs.com/bean/p/7689908.html

總結

以上是生活随笔為你收集整理的多生产者多消费者问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 三级在线观看网站 | 黄色片xxxx | 国产高清视频在线免费观看 | 欧美日韩生活片 | 114国产精品久久免费观看 | 免费亚洲一区二区 | 久青草影视 | 成人av在线播放网站 | 欧洲女女同性videoso | 看av免费毛片手机播放 | 免费看av大片 | 欧美大片视频在线观看 | 美女极度色诱图片www视频 | 麻豆私人影院 | 亚洲aaaa级特黄毛片 | 色图自拍| 色婷五月天 | 在线超碰91| 欧美视频在线观看免费 | 国产毛片毛片 | 国产成人无码精品 | 99国产在线视频 | 少妇一级淫片免费放中国 | a三级黄色片 | 欧美特黄视频 | 六月丁香激情综合 | 亚洲涩综合 | 国产性猛交╳xxx乱大交一区 | 永久免费看片在线播放 | 一本—道久久a久久精品蜜桃 | 在线观看国产精品一区 | 亚洲乱色熟女一区二区三区 | 一区二区三区四区不卡 | 日批毛片| 国产精品偷伦视频免费观看了 | 色国产视频 | jk美女又爽又黄视频 | 欧美sm凌虐视频网站 | 欧美两根一起进3p做受视频 | 国产精品一亚洲av日韩av欧 | 色片在线免费观看 | 黄色片网站在线播放 | 无码乱人伦一区二区亚洲 | 男人操女人网站 | 在线色网 | www.av成人| 日本一区二区三区网站 | 黑森林av导航 | 一级黄色大片网站 | 亚洲视频六区 | 亚洲成熟丰满熟妇高潮xxxxx | 韩国明星乱淫(高h)小说 | 综合视频在线观看 | 黄色网www | 天天干在线播放 | 亚洲天堂网在线观看视频 | 久热精品在线观看 | 激情综合文学 | 天天拍天天色 | 黄骗免费网站 | 国产精品999999| 亚洲 欧洲 日韩 | 青青操在线 | 2019天天干| 久热综合| 全黄性高潮 | 日韩一区二区视频在线 | 国产99精品 | 亚洲码欧美码一区二区三区 | 欧美v日本| 精品国产一级久久 | 欧美激情一二区 | 国产一区二区小视频 | 一区二区三区精品 | 亚洲第一在线视频 | 久精品在线 | 3d动漫精品啪啪一区二区三区免费 | 免费av在线播放 | 欧美日韩首页 | 日本精品99 | 麻豆影视在线播放 | 在线看日韩 | 色无五月| 一级在线免费视频 | 欧美在线网站 | 久久免费高清视频 | 狠狠干超碰| 欧美黑人又粗又大又爽免费 | 香蕉视频在线观看黄 | 国产小视频网址 | 国产精品一二三四五区 | 亚欧三级 | 亚洲精品97久久中文字幕 | 波多野结衣一区二区三区四区 | 成人h动漫精品一区二区下载 | av一区二区三区在线观看 | 一级a性色生活片久久毛片 爱爱高潮视频 | 日韩欧美一卡二卡 | 欧美bdsm调教视频 |