读《图解java多线程设计模式》总结--Producer-Consumer

1,从名字上看,就是多线程教程中最普遍的消费与生产问题。

2.适用范围,由于消费和生产分别由不同的线程来进行,这里就有一个速度同步的问题,有可能消费线程运行比较快,产品还没有被生产出来,也有可能消费线程运行比较慢,产品造成了积压,Producer-Consumer就是要解决消费线程与生产线程的同步问题。

3,实现方式,这里有3个概念,Producer生产者,负责生产产品,这里的产品可以是普通的数据,一个简单的实例对象等。Consumer消费者,负责消费生产者生产出来的产品。Channel通道,主要用于存放产品的地方,并且把产品提供给消费者消费,通道就是为了协调消费者与生产者之间的速度差,确保不会出现生产者与消费者不同步的情况发生。对于消费者和生产者,通道都会先检查保护条件,如果不满足保护条件就会让当前线程进入等待,如果满足就会执行操作,并且通知对方正在等待的线程。主要使用wait和notifyAll方法

4.代码实现

1)定义channer类,分别定义消费者使用的消费方法和生产者使用的生产方法,具体见代码注解

package producerconsumer.study;

import java.util.LinkedList;
public class Channel {

    private LinkedList<String> products=new LinkedList<String>();

    public synchronized void produce() throws InterruptedException {
        while(products.size()>=2)  //最多只生产2个产品,超过2个表示还没有被消费,生产线程需要等待
        {
            System.out.println("Too many product, producer wait consumer to consum");
            wait();
        }
        products.add("product1");  //生产产品
        products.add("product2");  //生产产品
        System.out.println("produce product");
        notifyAll();                //生产完了通知消费线程消费产品

    }


    public synchronized  void consum() throws InterruptedException {
        while(products.size()==0) //没有产品可供消费,消费线程需要等待
        {
            System.out.println("no product, consumer wait producer to produce");
            wait();
        }

        while(products.size()>0) {   //消费产品
            String product = products.pollFirst();
            System.out.println("consum product:"+product);
        }
        notifyAll();  //消费完了,通知等待的生产线程继续生产产品

    }
}

 

2) 分别启动消费线程和生产线程,分别调用通道类的消费方法和生产方法,各调用10次

package producerconsumer.study;

import java.util.Random;

public class ProducerConsumer {

    public static void main(String[] args) {
        Channel channel=new Channel();


        Thread producer=new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        Random random=new Random(5);
                        for(int i=0;i<10;i++) {
                            try {
                                channel.produce();
                                Thread.sleep(random.nextInt(100));//随机降低线程执行速度
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
        );


        Thread consumer=new Thread(
                new Runnable() {
                    @Override
                    public void run() {

                        Random random=new Random(4);
                        for(int i=0;i<10;i++) {
                            try {
                                channel.consum();
                                Thread.sleep(random.nextInt(100)); //随机降低线程执行速度
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
        );
        producer.start();
        consumer.start();

    }
}

 

3) 运行结果

//第一次调用结果

produce product
consum product:product1
consum product:product2

//第二次调用

no product, consumer wait producer to produce    //消费完了,没有产品,消费线程进入等待
produce product     //生产线程生产了产品,通知消费线程
consum product:product1 //消费线程消费产品1
consum product:product2   //消费线程消费产品1


no product, consumer wait producer to produce
produce product
consum product:product1
consum product:product2


no product, consumer wait producer to produce
produce product
consum product:product1
consum product:product2


produce product
Too many product, producer wait consumer to consum
consum product:product1
consum product:product2


produce product   //生产完了2个产品
Too many product, producer wait consumer to consum  //此时开始下一次调用,由于还没有被消费完,生产线程等待
consum product:product1   //消费产品1
consum product:product2  //消费产品2,消费完了,通知生产线程继续下一次的生产


produce product    //下一次生产完成 
consum product:product1 
consum product:product2


no product, consumer wait producer to produce
produce product
consum product:product1
consum product:product2


no product, consumer wait producer to produce
produce product
consum product:product1
consum product:product2


produce product
consum product:product1
consum product:product2

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本书浅显易懂的介绍了JAVA线程相关的设计模式,通过程序范例和UML图示来一一解说,书中代码的重要部分加了标注以使者更加容易理解,再加上图文并茂,对于初学者还是程序设计高手来说,这都是一本学习和认识JAVA设计模式的一本好书。(注意,本资源附带书中源代码可供参考) 多线程与并发处理是程序设计好坏优劣的重要课题,本书通过浅显易懂的文字与实例来介绍Java线程相关的设计模式概念,并且通过实际的Java程序范例和 UML图示来一一解说,书中在代码的重要部分加上标注使者更加容易解,再配合众多的说明图解,无论对于初学者还是程序设计高手来说,这都是一本学习和认识设计模式非常难得的好书。 书中包含Java线程的介绍导、12个重要的线设计模式和全书总结以及丰富的附录内容。第一章相关线设计模式的介绍,都举一反三使者学习更有效。最后附上练习问题,让者可以温故而知新,能快速地吸收书中的精华,书中最后附上练习问题解答,方便者学习验证。 目录 漫谈UML UML 类图 类和层次结构的关系 接口与实现 聚合 访问控制 类间的关联性 顺序图 处理流程和对象间的协调 时序图 Introduction 1 Java语言的线Java语言的线程 何谓线程 明为追踪处理流程,实则追踪线程 单线程程序 多线程程序 Thread类的run方法和start方法 线程的启动 线程的启动(1)——利用Thread类的子类 线程的启动(2)——利用Runnable接口 线程的暂时停止 线程的共享互斥 synchronized方法 synchronized阻挡 线程的协调 wait set——线程的休息室 wait方法——把线程放入wait set notify方法——从wait set拿出线程 notifyAll方法——从wait set拿出所有线程 wait、notify、notifyAll是Object类的方法 线程的状态移转 跟线程有关的其他话题 重点回顾 练习问题 Introduction 2 多线程程序的评量标准 多线程程序的评量标准 安全性——不损坏对象 生存性——进行必要的处理 复用性——可再利用类 性能——能快速、大量进行处理 评量标准的总结 重点回顾 练习问题 第1章 Single Threaded Execution——能通过这座桥的,只有一个人 第2章 Immutable——想破坏它也没办法 第3章 Guarded Suspension——要等到我准备好喔 第4章 Balking——不需要的话,就算了吧 第5章 Producer-Consumer——我来做,你来用 第6章 Read-Write Lock——大家想看就看吧,不过看的时候不能写喔 第7章 read-Per-Message——这个工作交给你了 第8章 Worker Thread——等到工作来,来了就工作 第9章 Future——先给您这张提货单 第10章 Two-Phase Termination——快把玩具收拾好,去睡觉吧 第11章 Thread-Specific Storage——每个线程的保管箱 第12章 Active Object——接受异步消息的主动对象 总结线程程序设计模式语言 附录A 练习问题的解答 附录B Java的内存模型 附录C Java线程的优先级 附录D 线程相关的主要API 附录E 参考文献
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值