生产/消费模型

生产/消费模型,顾名思义,就是生产者制造产品,消费者购买产品,当然,这其中要有店面作为两者的中介了。在软件开发的过程中就是一个模块作为生产者产生数据,另一个模块作为消费者处理数据,这中间夹着的是缓冲区,用来存放生产者产生的数据以及消费者提取数据的地方。结构如下:
[img]http://dl2.iteye.com/upload/attachment/0089/8111/7e0780da-381f-3cc4-9e7c-ace4eaaf67cf.jpg[/img]
最开始看到生产/消费模型想到的是生产者产生东西给消费者,那么缓冲区有啥用呢?其实呢,存在即合理嘛,缓冲区的存在自然有其好处的,生产者和消费者不直接依赖可以降低程序的耦合,消费者代码改变不会影响到生产者,再者,生产者和消费者直接关联,生产者生产出来的东西要等到消费者的方法响应之后才能继续太浪费时间了,此外就是可以让两者分隔开来以免生产的太快太慢或者消费的太快太慢不能及时进行处理。大致明白之后还是以代码来说明吧。

//产品类
public class Thing {
private String name;
public synchronized void set(String name){
this.setName(name);
try{
Thread.sleep(200);
}catch(Exception e){
e.printStackTrace();
}
}
public synchronized void get(){
try{
this.getName();
Thread.sleep(200);
}catch(Exception e){
e.printStackTrace();
}

}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
}


//生产者
public class Producer extends Thread{
private Thing thing=null;
public Producer(Thing thing){
this.thing=thing;
}
public void run(){
super.run();
while(true){
for(int i=0;i<50;i++){
this.thing.set("产品");
System.out.println("生产产品"+(i+1));
}
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
}
}

//消费者
public class Customer extends Thread{
private Thing thing=null;
public Customer(Thing thing){
this.thing=thing;
}
public void run(){
super.run();
while(true){
try{
for(int i=0;i<50;i++){
this.thing.get();
System.out.println("消费了产品"+(i+1));
}
}catch(Exception e){
e.printStackTrace();
}

}
}
}

//测试
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Thing thing=new Thing();
new Producer(thing).start();
new Customer(thing).start();
}
}

运行结果:
[img]http://dl2.iteye.com/upload/attachment/0089/8116/21279c68-55f4-3213-a51d-48d638f40b5e.jpg[/img]
从结果可以看出存在着重复读取和存入的问题,还要继续改进。加入wait()和notify()方法,直接修改产品类Thing,添加标志值,通过判断完成等待和唤醒的操作。
修改后的Thing类如下:
//产品类
public class Thing {
private String name;
private boolean flag=true;//设置标志
public synchronized void set(String name){
this.setName(name);
if(flag=false){
try{
super.wait();
}catch(Exception e){
e.printStackTrace();
}
}
try{
Thread.sleep(200);
}catch(Exception e){
e.printStackTrace();
}
flag=false;//改变标志值,表示可以取走
super.notify();
}
public synchronized void get(){
if(flag){
try{
super.wait();
}catch(Exception e){
e.printStackTrace();
}
}
try{
Thread.sleep(200);
}catch(Exception e){
e.printStackTrace();
}
this.getName();
flag=true;
super.notify();
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
}

运行结果:
[img]http://dl2.iteye.com/upload/attachment/0089/8143/4dbf34f4-dec6-341b-9c79-2782702b338d.jpg[/img]
这样改变之后可以是生产者生产一个,消费者取走一个,但是这样的话生产者等待的时间加长,不利于资源的有效利用,缓冲区的功能未实现,要使用BlockingQueue.

//产品类
public class Thing {
private String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
}
//生产者
public class Producer extends Thread{
java.util.concurrent.BlockingQueue<Thing> list;
public Producer(java.util.concurrent.BlockingQueue<Thing> list){
this.list=list;
}
public void run(){
super.run();
int count=1;
while(true){
Thing thing=new Thing();
thing.setName("东西"+count);
try{
list.add(thing);
System.out.println("生产"+thing.getName());
count++;
Thread.sleep(200);
}catch(Exception e){
e.printStackTrace();
}
}
}
}

//消费者
public class Customer extends Thread{
java.util.concurrent.ArrayBlockingQueue<Thing> list;
public Customer(ArrayBlockingQueue<Thing> list) {
this.list=list;
}
public void run(){
super.run();
while(true){
Thing thing;
try{
thing=list.take();
System.out.println("消费了"+thing.getName());
}catch(Exception e){
e.printStackTrace();
}
}
}
}

运行结果:
[img]http://dl2.iteye.com/upload/attachment/0089/8151/1e672376-b7a2-3b1a-8c7a-dfa12a0574ec.jpg[/img]
这个程序还不够完善,还有超出队列长度,多生存者和消费者等等情况,嘻嘻,待续~~~
在电影放映场景中,我们可以将影院看作是一个生产者(Producer),负责播放电影;而观众则作为消费者(Consumer),需要等待电影开始才能进入观看。这可以利用Java并发中的同步原语`synchronized`、`wait()`和`notifyAll()`来模拟线程之间的通信。 1. **电影院模型**:创建一个影院类(如FilmHall),内部有一个放映室(Screen)和一个队列(Queue)。影院有播放电影(playMovie())的方法,当电影开始时,会调用screen的startScreening()方法。同时,有一个queue用于记录等待的观众。 ```java class FilmHall { private Screen screen; private Queue<Viewer> queue; public synchronized void playMovie() { // ... 播放电影操作 if (queue.isEmpty()) { screen.startScreening(); } else { wait(); // 当队列非空时,影院线程进入等待状态 } } // ... 其他方法 } ``` 2. **观众模型**:创建观众类(Viewer),有加入队伍(joinQueue())和离开座位(leaveSeat())的方法。当观众到达并请求入座时,会调用queue的offer()方法,并唤醒影院线程: ```java class Viewer { public void joinQueue(FilmHall hall) { hall.queue.offer(this); notifyAll(); // 观众入座后通知影院开始播放 } // ... 离开座位方法 } ``` 3. **线程交互**:影院线程和观众线程需要互斥地访问共享资源(queue)。当queue为空时,影院线程等待;当有新观众加入时,唤醒影院线程继续播放。 ```java public class Main { public static void main(String[] args) { FilmHall hall = new FilmHall(); Viewer viewer = new Viewer(); // 开始一个观看任务 new Thread(() -> viewer.joinQueue(hall)).start(); // 开始播放电影 hall.playMovie(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值