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

    数据

    package cn.lonecloud.procum;
    
    /**
     * @author lonecloud
     * @version v1.0
     * @date 上午11:00 2018/5/7
     */
    public class Data {
    
        private String data;
    
        public String getData() {
            return data;
        }
    
        public void setData(String data) {
            this.data = data;
        }
    }
    

      

    生产者

    package cn.lonecloud.procum;
    
    import java.util.Random;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.atomic.AtomicInteger;
    
    /**
     * @author lonecloud
     * @version v1.0
     * @date 上午10:58 2018/5/7
     */
    public class Provider implements Runnable {
      
        private BlockingQueue<Data> queue;
    
        private static AtomicInteger integer = new AtomicInteger(0);
    
        public Provider(BlockingQueue<Data> queue) {
            this.queue = queue;
        }
    
        Random random = new Random();
    
        @Override
        public void run() {
            while (true) {
                int i = random.nextInt(1000);
                try {
              //模拟数据耗时 Thread.sleep(i); } catch (InterruptedException e) { e.printStackTrace(); } Data data = new Data(); data.setData(String.valueOf(integer.getAndIncrement())); try { if (!queue.offer(data, 20, TimeUnit.SECONDS)) { System.out.println("offer error"); } } catch (InterruptedException e) { e.printStackTrace(); } } } }

     消费者

    package cn.lonecloud.procum;
    
    import java.util.Objects;
    import java.util.Random;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @author lonecloud
     * @version v1.0
     * @date 上午10:58 2018/5/7
     */
    public class Customer implements Runnable {
        private BlockingQueue<Data> queue;
    
        public Customer(BlockingQueue<Data> queue) {
            this.queue = queue;
        }
    
        Random random = new Random();
    
        @Override
        public void run() {
            while (true) {
                try {
                    Data data = queue.take();
                    System.out.println("---------"+data.getData());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int i = random.nextInt(1000);
                try {
                    Thread.sleep(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

     主函数

    package cn.lonecloud.procum;
    
    import cn.lonecloud.thread.factory.TraceThreadPool;
    
    import java.util.concurrent.*;
    
    /**
     * @author lonecloud
     * @version v1.0
     * @date 上午11:06 2018/5/7
     */
    public class Main {
        public static void main(String[] args) {
            BlockingQueue queue = new ArrayBlockingQueue(10);
    //        ThreadPoolExecutor poolExecutor=new TraceThreadPool(5,5,20, TimeUnit.SECONDS,new ArrayBlockingQueue<>(10));
            ExecutorService poolExecutor=Executors.newFixedThreadPool(5);
            for (int i = 0; i < 3; i++) {
                Customer customer = new Customer(queue);
                poolExecutor.submit(customer);
            }
            for (int i = 0; i < 5; i++) {
                Provider provider = new Provider(queue);
                poolExecutor.submit(provider);
            }
        }
    }
    

      

  • 相关阅读:
    Mysql集群
    JAVA 经典算法 40 例
    公司面试问题总结
    面试题6
    面试题5
    Java自学-JDK环境变量配置
    mybatis中#{}和${}的区别
    JVM系列(四)— 原子性、可见性与有序性
    JVM系列(三)— Java内存模型
    Java基础拾遗(一) — 忽略的 Integer 类
  • 原文地址:https://www.cnblogs.com/lonecloud/p/9002765.html
Copyright © 2011-2022 走看看