zoukankan      html  css  js  c++  java
  • 生产者消费者模式实现

    生产者与消费者解耦,典型应用:MQ。不多解释,code talking:


    调用模块:

    package com.array7.ds.pc;
    
    import java.util.concurrent.BlockingDeque;
    import java.util.concurrent.LinkedBlockingDeque;
    
    public class Run {
    	public static void main(String[] args) {
    		BlockingDeque<Integer> queue = new LinkedBlockingDeque<Integer>(50);
    		int stopConditon = 100;
    		new Productor(queue).begin(stopConditon);
    		new Customer(queue).begin(stopConditon);
    		
    	}
    }
    


    生产者模块:

    package com.array7.ds.pc;
    
    import java.util.concurrent.BlockingDeque;
    import java.util.concurrent.atomic.AtomicInteger;
    
    public class Productor {
    	static AtomicInteger flag = new AtomicInteger(0);
    	private BlockingDeque<Integer> queue;
    
    	public Productor(BlockingDeque<Integer> queue) {
    		this.queue = queue;
    	}
    
    	public void begin(int stopConditon) {
    		new Thread(new Create(queue, stopConditon)).start();
    	}
    
    	public static class Create implements Runnable {
    		private BlockingDeque<Integer> queue;
    		private int stopConditon;
    
    		public Create(BlockingDeque<Integer> queue, int stopConditon) {
    			this.queue = queue;
    			this.stopConditon = stopConditon;
    		}
    
    		@Override
    		public void run() {
    			while (true) {
    				try {
    					Thread.sleep(5);
    					int i = flag.incrementAndGet();
    					if (i == stopConditon) {
    						queue.put(i);
    						System.out.println("stop put element >>> ");
    						break;
    					}
    					queue.put(i);
    					System.out.println("put element >>> " + i);
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    
    	}
    }
    


    消费者模块

    package com.array7.ds.pc;
    
    import java.util.concurrent.BlockingDeque;
    
    public class Customer {
    	BlockingDeque<Integer> queue;
    
    	public Customer(BlockingDeque<Integer> queue) {
    		this.queue = queue;
    	}
    
    	public void begin(int stopConditon) {
    		new Thread(new Get(queue, stopConditon)).start();
    	}
    
    	public static class Get implements Runnable {
    		private BlockingDeque<Integer> queue;
    		private int stopConditon;
    
    		public Get(BlockingDeque<Integer> queue, int stopConditon) {
    			this.queue = queue;
    			this.stopConditon = stopConditon;
    		}
    
    		@Override
    		public void run() {
    			while (true) {
    				try {
    
    					Thread.sleep(10);
    					Integer e = queue.take();
    					if (e != null) {
    						if (e.intValue() == stopConditon) {
    							System.out.println("stop get element >>>>>> ");
    							break;
    						}
    						System.out.println("get element >>>>>> " + e);
    					}
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    
    	}
    }
    



    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    蓝鸥0c考试 绝密
    省市县用对象实现
    省市区用字典实现
    3道比较有用的oc练习题
    ios 开发 OC编程 类的扩展 协议 延展和 类目
    ios 开发 OC编程 内存管理
    ios 开发 OC编程 块语法bolck的一些应用
    ios 开发 OC编程 块语法bolck
    中等难度的通讯录.字典 动态分组法
    TestFlight
  • 原文地址:https://www.cnblogs.com/liushijie/p/4712910.html
Copyright © 2011-2022 走看看