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

    结果

  • 相关阅读:
    对Spring <context:annotation-config/>的理解
    Javascript this指针
    go 打造世界最快的go模板引擎gorazor 2.0
    swagger 部署(Mac )
    Ab测试
    Nginx tcp限制并发、IP、记日志
    Nginx proxy_protocol协议与realip模块
    数据结构之回溯
    数据结构之分治
    数据结构之二分查找
  • 原文地址:https://www.cnblogs.com/tk55/p/8580451.html
Copyright © 2011-2022 走看看