多线程设计模式之——Producer-Consumer Pattern
2013-02-28 12:36
176 查看
此模式是在生产者与消费者之间加入一个“桥梁参与者”来缓冲线程之间的处理速度差。一般可以存在多个生产者与消费者,但当双方都只有一个的时候,又称为Pipe Pattern。
例子:假设有2个生产者,2个消费者,仓库里只能放4个产品。(这里的产品就是String类型的名字而已)
Java代码
//Storage.java
public class Storage {
private String[] buffer;
private int head;//取走一个商品的位置
private int tail;//存入一个商品的位置
private int count;//buffer内的商品数量
public Storage(int count){
this.buffer = new String[count];
this.count = 0;
this.head = 0;
this.tail = 0;
}
//这里的if警戒条件就运用了Guarded Suspension Pattern,要求不满足条件,便等待
public synchronized void put(String goods) throws InterruptedException{
System.out.println(Thread.currentThread().getName() + " produce the goods:" + goods);
if(count >= buffer.length){
System.out.println("the storage is full!");
wait();
}
buffer[tail] = goods;
tail = (tail + 1) % buffer.length;
count++;
notifyAll();
}
public synchronized String get() throws InterruptedException{
if(count <= 0){
System.out.println("the storage is empty!");
wait();
}
String goods = buffer[head];
head = (head + 1) % buffer.length;
count--;
notifyAll();
System.out.println(Thread.currentThread().getName() + " consume the goods:" + goods );
return goods;
}
}
Java代码
//ProducerThread.java
import java.util.Random;
public class ProducerThread extends Thread {
private Storage storage;
private Random random;
private static int pid; //产品编号
public ProducerThread(String name , Storage storage){
super(name);
this.storage = storage;
this.random = new Random();
}
public void run(){
try{
for(int i=0; i<10; i++){
Thread.sleep(random.nextInt(1000));//模拟生产时间
String goods = getName() + " produce the goods" + nextPId();
storage.put(goods);
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
private static synchronized int nextPId(){
return pid++;
}
}
Java代码
//ConsumerThread.java
import java.util.Random;
public class ConsumerThread extends Thread {
private Storage storage;
private Random random;
public ConsumerThread(String name , Storage storage){
super(name);
this.random = new Random();
this.storage = storage;
}
public void run(){
try{
while(true){
storage.get();
Thread.sleep(random.nextInt(2000));//模拟商品使用时间
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
Java代码
//Main.java
public class Main {
public static void main(String[] args) {
Storage storage = new Storage(4);
new ProducerThread("producer_lulu01" , storage).start();
new ProducerThread("producer_lulu02" , storage).start();
new ConsumerThread("consumer_fang01" , storage).start();
new ConsumerThread("consumer_fang02" , storage).start();
}
}
例子:假设有2个生产者,2个消费者,仓库里只能放4个产品。(这里的产品就是String类型的名字而已)
Java代码
//Storage.java
public class Storage {
private String[] buffer;
private int head;//取走一个商品的位置
private int tail;//存入一个商品的位置
private int count;//buffer内的商品数量
public Storage(int count){
this.buffer = new String[count];
this.count = 0;
this.head = 0;
this.tail = 0;
}
//这里的if警戒条件就运用了Guarded Suspension Pattern,要求不满足条件,便等待
public synchronized void put(String goods) throws InterruptedException{
System.out.println(Thread.currentThread().getName() + " produce the goods:" + goods);
if(count >= buffer.length){
System.out.println("the storage is full!");
wait();
}
buffer[tail] = goods;
tail = (tail + 1) % buffer.length;
count++;
notifyAll();
}
public synchronized String get() throws InterruptedException{
if(count <= 0){
System.out.println("the storage is empty!");
wait();
}
String goods = buffer[head];
head = (head + 1) % buffer.length;
count--;
notifyAll();
System.out.println(Thread.currentThread().getName() + " consume the goods:" + goods );
return goods;
}
}
Java代码
//ProducerThread.java
import java.util.Random;
public class ProducerThread extends Thread {
private Storage storage;
private Random random;
private static int pid; //产品编号
public ProducerThread(String name , Storage storage){
super(name);
this.storage = storage;
this.random = new Random();
}
public void run(){
try{
for(int i=0; i<10; i++){
Thread.sleep(random.nextInt(1000));//模拟生产时间
String goods = getName() + " produce the goods" + nextPId();
storage.put(goods);
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
private static synchronized int nextPId(){
return pid++;
}
}
Java代码
//ConsumerThread.java
import java.util.Random;
public class ConsumerThread extends Thread {
private Storage storage;
private Random random;
public ConsumerThread(String name , Storage storage){
super(name);
this.random = new Random();
this.storage = storage;
}
public void run(){
try{
while(true){
storage.get();
Thread.sleep(random.nextInt(2000));//模拟商品使用时间
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
Java代码
//Main.java
public class Main {
public static void main(String[] args) {
Storage storage = new Storage(4);
new ProducerThread("producer_lulu01" , storage).start();
new ProducerThread("producer_lulu02" , storage).start();
new ConsumerThread("consumer_fang01" , storage).start();
new ConsumerThread("consumer_fang02" , storage).start();
}
}
相关文章推荐
- 多线程设计模式——Producer-Consumer生产者消费者模式
- 多线程设计模式之Producer Consumer模式
- Java多线程之并发协作生产者消费者设计模式
- (一)java多线程编程设计模式之不可变模式
- 多线程设计模式总结(三)
- 架构师入门笔记四 初识多线程设计模式
- 设计模式——单例模式(Java)——考虑多线程环境下的线程安全问题
- 多线程编程的设计模式 不变模式(一)
- JAVA之旅(十四)——静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制
- 多线程设计模式——Thread Pool(线程池)模式
- Java 多线程编程设计模式之Immutable Object 模式
- java基础—多线程下的单例设计模式的安全问题
- 多线程设计模式
- 设计模式-单例模式之饿汉式-多线程
- 【多线程高并发】多线程的设计模式
- 多线程的设计模式之Master-Worker模式
- 多线程设计模式Future、Master-Worker和生产者-消费者模型
- Android多线程设计模式---wait()和notify()机制
- Java中的面试题 [3] --- Java高级知识、并发、多线程、JVM调优、分布式、设计模式等
- 设计模式与多线程——用命令模式来设计多线程架构