设计模式学习之——生产消费模式
来源:互联网 发布:最好笑知乎 编辑:程序博客网 时间: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
- 设计模式学习之——生产消费模式
- 生产消费设计模式
- 生产消费设计模式
- ActiveMQ 由浅入深之二(生产消费模式)
- java多线程之生产消费模式
- 生产消费模式之数据结构策略
- 线程生产消费模式
- 生产消费模式
- 生产消费模式
- 生产/消费模式实现
- 设计模式——生产消费者模式
- blockingQueue实现消费-生产模式
- 多线程初探之使用Lock实现生产-消费模式
- 设计模式学习之—观察者模式
- JAVA生产消费模式(代码及流程图)
- Java多线程实例->生产消费模式
- 线程的简单生产消费模式
- 生产消费模式的几种实现
- java对Date的年月日的加减运算
- python学习(仅作为个人学习笔记,记录零散知识点)
- Qt ScrollArea不出现滚动条及滚动条失效的解决办法以及所有控件保持最小尺寸而不再缩小的方法
- CentOS 7 安装VirtualBox
- 视觉跟踪项目(三)
- 设计模式学习之——生产消费模式
- 完整学习使用CSS动画【翻译】
- swift3 控制流
- 数据库分库分表(sharding)拆分实施策略和示例
- 杭电acm 2059 龟兔赛跑
- CentOS6.5菜鸟之旅:安装VirtualBox4.3
- WebStorm如何让浏览器实时更新
- jquery 常用技巧
- head first html与css学习笔记(一)