zoukankan      html  css  js  c++  java
  • java多线程系列15 设计模式 生产者

    生产者-消费者 

      生产者消费者模式是一个非常经典的多线程模式,比如我们用到的Mq就是其中一种具体实现

      在该模式中 通常会有2类线程,消费者线程和生产者线程

     生产者提交用户请求 消费者负责处理生产者提交的任务,在消费者和生产者之间共享内存缓存区进行通信

      常见的实现 可以 通过 wait/notifyAll来  或者 阻塞队列来实现 下面我来演示下通过 wait/notifyAll 来实现。。。

    下面是代码演示

    public class Storage<T> {
    	LinkedList<T> list = new LinkedList<>();
    	private Integer maxSize;
    
    	public Integer getMaxSize() {
    		return maxSize;
    	}
    
    	public void setMaxSize(Integer maxSize) {
    		this.maxSize = maxSize;
    	}
    
    	public T consumer() {
    		synchronized (list) {
    			if (list == null || list.size() == 0) {
    				try {
    					System.out.println(Thread.currentThread().getName() + " 等待  ");
    					list.wait();
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    			} else {
    				T t = list.remove();
    				try {
    					Thread.sleep(200);
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    				System.out.println(Thread.currentThread().getName() + " 消费  " + t);
    				if (list.size() == 0) { // 消费完了 通知生产者 继续生产
    					list.notifyAll();
    				}
    				return t;
    			}
    		}
    		return null;
    	}
    
    	public void producer(T t) {
    		synchronized (list) {
    			if (list.size() == maxSize.intValue()) {
    				System.out.println(Thread.currentThread().getName() + " 仓库已满 暂停生产  ");
    				try {
    					list.wait();
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    			} else {
    				list.add(t);
    				System.out.println(Thread.currentThread().getName() + " 生产  " + t);
    
    				list.notifyAll();
    			}
    
    		}
    	}
    
    	public static void main(String[] args) {
    		Storage<Integer> storage = new Storage<>();
    		storage.setMaxSize(5);
    		AtomicInteger numberGenarnate = new AtomicInteger(0);
    
    		ExecutorService consumerService = Executors.newCachedThreadPool();
    		for (int i = 0; i < 3; i++) {
    			Runnable run = new Runnable() {
    
    				@Override
    				public void run() {
    					while (true) {
    						storage.consumer();
    					}
    				}
    			};
    			consumerService.submit(run);
    		}
    
    		for (int i = 0; i < 1; i++) {
    			Runnable run = new Runnable() {
    
    				@Override
    				public void run() {
    					while (true) {
    						storage.producer(numberGenarnate.incrementAndGet());
    					}
    				}
    			};
    			consumerService.submit(run);
    		}
    
    		consumerService.shutdown();
    	}
    }

    运行结果如下

     

      总结:对于消费者生产者模式 要理解其思想。实际开发中。mq(消息队列)就是典型的应用。

           对于mq这里多说几句,关于技术选型:::

           Mq适用于生产者生产很多   消费者处理不过来的情况 。如果消费者处理能力很强,就不要用mq了,直接使用nio框架(mina   or  netty

  • 相关阅读:
    php 下载文件
    thinkphp3.1 发送email
    微擎 plugin 时间插件 图片上传插件不显示 报错 影响下面执行
    Java中基本数据类型的对比记忆
    堆内存设置以及垃圾回收方式
    try--catch--finally中return返回值执行的顺序(区别)
    Java中的值传递和引用传递
    全面总结sizeof的用法(定义、语法、指针变量、数组、结构体、类、联合体、位域位段)
    10进制转换成16进制最简单的方法
    quartz 框架定时任务,使用spring @Scheduled注解执行定时任务
  • 原文地址:https://www.cnblogs.com/javabigdata/p/7028798.html
Copyright © 2011-2022 走看看