zoukankan      html  css  js  c++  java
  • 生产者消费者模型

    1.篮子

    package queue.test;
    
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.LinkedBlockingQueue;
    
    public class Basket {
    
        BlockingQueue<String> basket = new LinkedBlockingQueue<String>(3);
        
        public void produce() throws InterruptedException
        {
            basket.put("apple");
        }
        
        public String consume() throws InterruptedException
        {
            return basket.take();
        }
    }

    2.生产者

    package queue.test;
    
    public class Producer implements Runnable{
    
        private String instance;
        private Basket basket;
    
        public Producer(String instance, Basket basket) {
            super();
            this.instance = instance;
            this.basket = basket;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while(true)
            {
                System.out.println("produce apple start-"+instance);
                try 
                {
                    basket.produce();
                    System.out.println("produce apple end-"+instance);
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
    }

    3.消费者

    package queue.test;
    
    public class Consumer implements Runnable{
    
        private String instance;
        private Basket basket;
        
        public Consumer(String instance, Basket basket) {
            super();
            this.instance = instance;
            this.basket = basket;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while(true)
            {
                System.out.println("consumer start-"+instance);
                try {
                    basket.consume();
                    
                    System.out.println("consumer end-"+instance);
                    
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
    }

    3.测试

    package queue.test;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class QueueTest {
    
        public static void main(String[] args) {
            Basket basket = new Basket();
            
            Producer p1 = new Producer("p1", basket);
            Producer p2 = new Producer("p2", basket);
            Consumer c = new Consumer("c1", basket);
            
            ExecutorService pool = Executors.newCachedThreadPool();
            pool.submit(p1);
            pool.submit(p2);
            pool.submit(c);
        }
    
    }
  • 相关阅读:
    网站名记录
    Unity 之 图片显示的真实大小
    Unity 之 Time
    数据库死锁分析与解决
    使用命令远程注销服务器
    Web自动化测试框架改进
    Tomcat 性能监控及调优
    移动互联网安全性测试技术简介
    白盒测试中如何实现真正意义上并发测试(Java)
    接口性能测试方案 白皮书 V1.0
  • 原文地址:https://www.cnblogs.com/Android9527/p/5407340.html
Copyright © 2011-2022 走看看