设计模式学习之——生产消费模式

来源:互联网 发布:最好笑知乎 编辑:程序博客网 时间:2024/05/16 01:10

这种模式并没有列入23种设计模式,但是也时一种经典的模式

//首先是生产者
 //生产者public class Priducter implements Runnable {    private ArrayList<Product> list;    int id;    public Priducter (ArrayList<Product> list){        this.list = list;    }    @Override    public void run() {        while (true){if(list == null){continue;}    //加锁        synchronized (list) {//若果生产苹果过多,超过100个就暂停生产        if (list.size() >100){           try {                 list.wait();               } catch (InterruptedException e) {                 e.printStackTrace();               }              }                id++;                Product product = new Apple("红富士", id);                list.add(product);                System.out.println("生产苹果:目前有"+list.size());                list.notifyAll();//通知消费者吃苹果            }            try {                Thread.sleep(300);            } catch (Exception e) {                e.printStackTrace();            }        }    }}
//然后是消费者package two;import java.util.ArrayList;/** * Created by Administrator on 2017/3/3. */public class Customer implements Runnable {    private ArrayList<Product> list;    public Customer (ArrayList<Product> list){        this.list = list;    }    @Override    public void run() {        while (true){            if (list == null) {                continue;            }            synchronized (list) {//当苹果被吃完后就可以线休息休息了,进入等待状态                if (list.size() == 0) {                    try {                        list.wait();                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }//当剩余苹果不足10个时通知生产者继续生产                if (list.size()<10){                    list.notifyAll();                }//吃苹果                System.out.println("吃掉第" + list.get(0).id + "个苹果");                list.remove(0);            }            try {                Thread.sleep(400);            } catch (Exception e) {                e.printStackTrace();            }        }    }}
//产品的抽象父类package two;/** * Created by Administrator on 2017/3/3. */public abstract class Product {    String name;    int id;}
//具体的产品类——苹果package two;/** * Created by Administrator on 2017/3/3. */public class Apple extends Product {    public Apple (String name,int id){        this.name = name;        this.id = id;    }}
//开始运行package com.example.shengchanxiaofei;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import java.util.ArrayList;import two.Priducter;import two.Product;public class MainActivity extends AppCompatActivity {    ArrayList<Product> list;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        list = new ArrayList<>();        Thread lin1 = new Thread(new Priducter(list));        Thread lin2 = new Thread(new two.Customer(list));        lin1.start();        lin2.start();    }}

具体的实践:由于不方便透漏项目源码,所以只简单说下

当时app中使用的网络链接不是http协议,也就没有使用第三方网络框架,需要自己用socket来实现。考虑到网络交互是耗时操作,所以网络链接采用了生产消费模式。主要是响应服务器数据时,将服务器返回的每条数据(产品)都存入队列,每次接到数据就会唤醒消费者(一个把数据用handler发送到主线程的Thread)来执行。

0 0
原创粉丝点击