Java BlockingQueue生产者消费者实例

  • 产品类:

    /**
     * 产品
     *
     * @author jiazhipeng
     * @version 1.0
     * @date 2016-11-29
     */
    public class Product {
    
        private int id;
    
        public Product(int id){
            this.id = id;
        }
    
        public String toString(){
            return "产品" + id;
        }
    }
  • 仓库类:

    /**
     * 仓库
     *
     * @author jiazhipeng
     * @version 1.0
     * @date 2016-11-29
     */
    public class Storage {
    
        private BlockingQueue<Product> queue = new ArrayBlockingQueue<>(10);
    
        public synchronized void put(Product p) throws InterruptedException{
            queue.put(p);
        }
    
        public Product take() throws InterruptedException{
            return queue.take();
        }
    }
  • 生产者类:

    /**
     * 生产者
     *
     * @author jiazhipeng
     * @version 1.0
     * @date 2016-11-29
     */
    public class Producter implements Runnable{
    
        // 自己的信号量
        private Semaphore semo1;
        // 别人的信号量
        private Semaphore semo2;
        private String name;
        private Storage storage;
        private Test test;
    
        public Producter(Storage storage, Test test, String name, Semaphore semo1, Semaphore semo2){
            this.storage = storage;
            this.test = test;
            this.name = name;
            this.semo1 = semo1;
            this.semo2 = semo2;
        }
    
        @Override
        public void run() {
            for(; ; ){
                try {
                    semo1.acquire();
                    int i = test.getInt();
                    Product p = new Product(i);
                    storage.put(p);
                    System.out.println(name + "成功生产:" + p.toString());
                    semo2.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
    }
  • 消费者类:

    /**
     * 消费者
     *
     * @author jiazhipeng
     * @version 1.0
     * @date 2016-11-29
     */
    public class Consumer implements Runnable{
    
        private String name;
        private Storage storage;
    
        public Consumer(Storage storage, String name){
            this.storage = storage;
            this.name = name;
        }
    
        @Override
        public void run() {
            for(; ; ){
                try {
                    Product p = storage.take();
                    System.out.println(name + "成功消费:" + p.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
    }
  • 测试类:

    /**
     * 测试
     *
     * @author jiazhipeng
     * @version 1.0
     * @date 2016-11-29
     */
    public class Test {
    
        private int i = 1;
    
        public static void main(String[] args) {
            Test test = new Test();
            Storage storage = new Storage();
    
            Semaphore semo1 = new Semaphore(1);
            Semaphore semo2 = new Semaphore(0);
    
            Producter pr1 = new Producter(storage, test, "A1", semo1, semo2);
            Producter pr2 = new Producter(storage, test, "A2", semo2, semo1);
            Consumer cr1 = new Consumer(storage, "B1");
            Consumer cr2 = new Consumer(storage, "B2");
    
            ExecutorService service = Executors.newCachedThreadPool();
            service.submit(pr1);
            service.submit(pr2);
            service.submit(cr1);
            service.submit(cr2);
        }
    
        public synchronized int getInt(){
            return i++;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值