多生产者多消费者问题
生活随笔
收集整理的這篇文章主要介紹了
多生产者多消费者问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*** 多生產者 多消費者問題* * 該代碼存在死鎖問題* 因為有可能喚醒本方 * @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
總結
以上是生活随笔為你收集整理的多生产者多消费者问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AtCoder Regular Cont
- 下一篇: python升级或者其他原因把yum搞坏