zoukankan      html  css  js  c++  java
  • 阻塞自定义队列

    import java.util.LinkedList;
    import java.util.List;
    
    /**
     * Created by Admin on 2018/3/16.
     */
    public class BlockingQueue{
        private List queue=new LinkedList();
        private int size=10;
        public BlockingQueue(int size){
            this.size=size;
        }
    
        public BlockingQueue() {
    
        }
        public int getSize(){
            return this.queue.size();
        }
    
        public synchronized  Object poll()throws InterruptedException{
            while (this.queue.size()==0){
                wait();
            }
            if (this.queue.size()==this.size) {
                notifyAll();
            }
            return this.queue.remove(0);
       }
        public synchronized  void push(Object emum) throws InterruptedException{
           while (this.queue.size()==this.size){
               wait();
           }
           if (this.queue.size()==0){
               notifyAll();
           }
           this.queue.add(emum);
        }
    }

    测试

    /**
     * Created by Admin on 2018/3/16.
     */
    public class TestBlockingQueue {
        public static void main(String[] args) throws InterruptedException {
            BlockingQueue bq=new BlockingQueue();
            bq.push("b");
            bq.push("ba");
            bq.push("baa");
            bq.push("aaab");
            System.out.println(bq.getSize());
            System.out.println(bq.poll());
            System.out.println(bq.getSize());
            System.out.println(bq.poll());
            System.out.println(bq.getSize());
            System.out.println(bq.poll());
            System.out.println(bq.getSize());
            System.out.println(bq.poll());
            System.out.println(bq.getSize());
            System.out.println(bq.poll());
        }
    }

    结果

  • 相关阅读:
    MySQL中mysqldump导出数据的使用
    MySQL中show语法使用总结
    Nginx配置项优
    Elasticsearch5.2.2安装
    SSH实现双向认证
    MySQL5.6主从复制搭建基于日志(binlog)
    清除Pycharm设置的方法
    Python3编程技巧
    django组件-中间件
    Python打杂之路
  • 原文地址:https://www.cnblogs.com/tk55/p/8580451.html
Copyright © 2011-2022 走看看