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

    结果

  • 相关阅读:
    资源链接
    python pip下载速度慢的解决方法
    淘宝 NPM 镜像
    python学习链接
    Linux升级python3之后yum不能正常使用解决方法一:重新配置yum源
    rand和srand的用法
    static与volatile的用法
    CentOS 7
    C++类(Class)总结
    简单的linux命令
  • 原文地址:https://www.cnblogs.com/tk55/p/8580451.html
Copyright © 2011-2022 走看看