java生成者消费者队列实现(全)
来源:互联网 发布:小可知乎 编辑:程序博客网 时间:2024/04/28 01:45
单一生产,单一消费:
信号量方式:
package test;public class Main {boolean flag = true;int food = 0;class T1 extends Thread{@Overridepublic void run() {while(true){synchronized (this) {while(!flag){try {wait();} catch (InterruptedException e) {e.printStackTrace();}}}synchronized (t2) {food++;System.out.println("make "+food);flag=!flag;t2.notify();}}}}class T2 extends Thread{@Overridepublic void run() {while(true){synchronized (this) {while(flag){try {wait();} catch (InterruptedException e) {e.printStackTrace();}}}synchronized (t1) {food--;System.out.println("eat "+food);flag=!flag;t1.notify();}}}}T1 t1 = new T1();T2 t2 = new T2();public static void main(String[] args) {Main m = new Main();m.t1.start();m.t2.start();}}
信号集
package test;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class Test{ReentrantLock lock1 = new ReentrantLock();//满锁ReentrantLock lock2 = new ReentrantLock();//空锁Condition c1 = lock1.newCondition();Condition c2 = lock2.newCondition();int food = 0;class T1 extends Thread{//生产者@Overridepublic void run() {while(true){lock1.lock();while(food==1){try {c1.await();} catch (InterruptedException e) {e.printStackTrace();}}lock1.unlock();lock2.lock();System.out.println("生产"+(++food));c2.signalAll();lock2.unlock();}}}class T2 extends Thread{//消费者@Overridepublic void run() {while(true){lock2.lock();while(food==0){try {c2.await();} catch (InterruptedException e) {e.printStackTrace();}}lock2.unlock();lock1.lock();System.out.println("消费"+(--food));c1.signalAll();lock1.unlock();}}}T1 t1 = new T1();T2 t2 = new T2();public static void main(String[] args) {Test m = new Test();m.t1.start();m.t2.start();}}
生产者消费者队列:
信号量方式:
package test;import java.util.Arrays;public class Main {class Food{//食物类int num = 0;Food(int num){this.num = num;}@Overridepublic String toString() {return "Food"+this.num;}}int max = 10;int count = 0;//当前队列里面的Food数目Food[] foods = new Food[max];class T1 extends Thread{//生产者@Overridepublic void run() {while(true){synchronized (this) {while(count==max){try {wait();} catch (InterruptedException e) {e.printStackTrace();}}}synchronized (t2) {foods[count] = new Food(count);count++;System.out.println("生产"+Arrays.toString(foods));t2.notify();}}}}class T2 extends Thread{//消费者@Overridepublic void run() {while(true){synchronized (this) {while(count==0){try {wait();} catch (InterruptedException e) {e.printStackTrace();}}}synchronized (t1) {foods[--count] = null;System.out.println("消费"+Arrays.toString(foods));t1.notify();}}}}T1 t1 = new T1();T2 t2 = new T2();public static void main(String[] args) {Main m = new Main();m.t1.start();m.t2.start();}}信号集
package test;import java.util.Arrays;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class Main {class Food{//食物类int num = 0;Food(int num){this.num = num;}@Overridepublic String toString() {return "Food"+this.num;}}ReentrantLock lock1 = new ReentrantLock();//满锁ReentrantLock lock2 = new ReentrantLock();//空锁Condition c1 = lock1.newCondition();Condition c2 = lock2.newCondition();int max = 10;int count = 0;//当前队列里面的Food数目Food[] foods = new Food[max];class T1 extends Thread{//生产者@Overridepublic void run() {while(true){lock1.lock();while(count==max){try {c1.await();} catch (InterruptedException e) {e.printStackTrace();}}lock1.unlock();lock2.lock();foods[count] = new Food(count);count++;System.out.println("生产"+Arrays.toString(foods));c2.signalAll();lock2.unlock();}}}class T2 extends Thread{//消费者@Overridepublic void run() {while(true){lock2.lock();while(count==0){try {c2.await();} catch (InterruptedException e) {e.printStackTrace();}}lock2.unlock();lock1.lock();foods[--count] = null;System.out.println("消费"+Arrays.toString(foods));c1.signalAll();lock1.unlock();}}}T1 t1 = new T1();T2 t2 = new T2();public static void main(String[] args) {Main m = new Main();m.t1.start();m.t2.start();}}
管程方式
package test;import java.util.Arrays;public class Main {class Food{//食物类int num = 0;Food(int num){this.num = num;}@Overridepublic String toString() {return "Food"+this.num;}}int max = 10;int count = 0;//当前队列里面的Food数目static Main m = new Main();Food[] foods = new Food[max];synchronized void produce(){while(count==max){try {wait();} catch (InterruptedException e) {e.printStackTrace();}}foods[count] = new Food(count);count++;System.out.println("生产"+Arrays.toString(foods));notifyAll();}synchronized void consume(){while(count==0){try {wait();} catch (InterruptedException e) {e.printStackTrace();}}foods[--count] = null;System.out.println("消费"+Arrays.toString(foods));notifyAll();}class T1 extends Thread{//生产者@Overridepublic void run() {while(true){m.produce();}}}class T2 extends Thread{//消费者@Overridepublic void run() {while(true){m.consume();}}}T1 t1 = new T1();T2 t2 = new T2();public static void main(String[] args) {Main m = new Main();m.t1.start();m.t2.start();}}
0 0
- java生成者消费者队列实现(全)
- 生产者消费者JAVA队列实现
- Java 队列 实现生产者-消费者模式
- java实现消费者与生产者队列
- vc实现的 生成者,消费者问题
- java中自动实现生产者消费者模式的队列
- Java生产者-消费者模式的非阻塞队列实现
- Java生产者-消费者模式的阻塞队列实现
- JAVA 阻塞队列实现 生产者和消费者 模式
- java使用阻塞队列实现生产者消费者模式
- Java并发编程:阻塞队列及实现生产者-消费者模式
- Java线程实现Redis任务队列(生产者消费者)
- 实现一个生产者消费者队列
- 队列实现生产者消费者模型
- 阻塞队列实现生产者消费者
- Java synchronized、wait、notify实现线程(生成消费者模式)
- 全排列生成算法java实现
- 归纳法-生成全排列算法Java实现
- CSU1600:Twenty-four point(DFS)
- Arc函数使用说明
- java远程debug
- my eclipse 的代码提示功能
- UVA - 656 Optimal Programs
- java生成者消费者队列实现(全)
- NSDictionary和NSMutableDictionary
- bzoj 1862/1056 [HAOI2008]排名系统
- 仿手机酷狗-音乐播放页面拖动效果
- CSU1604: SunnyPig(BFS)
- Android中的Audio播放:音量和远程播放控制
- JSP文件是否有必要放在WEB-INF下
- 使用L脚本语言实现分布式计算的例子
- 置顶