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

这种模式并没有列入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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值