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();
    				}
    			}
    		}
    
    	}
    }
    



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

  • 相关阅读:
    .Net C# ASP.Net和ADO.Net
    如何学习.Net的步骤
    PHP5.2.17版本 fgetcsv函数 读取中文bug
    大数据入门至精通视频集
    Rethinking Table Recognition using Graph Neural Networks
    GRAPH ATTENTION NETWORKS(GAT)图注意力网络
    六个步骤快速学习难以掌握的资料
    学会总结
    数据结构学习-AVL平衡树
    数据结构学习-BST二叉查找树 : 插入、删除、中序遍历、前序遍历、后序遍历、广度遍历、绘图
  • 原文地址:https://www.cnblogs.com/liushijie/p/4712910.html
Copyright © 2011-2022 走看看