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

    结果

  • 相关阅读:
    uva 804WAWAWA--不想看了以后再说
    uva10129 play on words
    tree--
    打印素数表orz
    DeepFM模型
    国内常用镜像链接
    Thompson(汤普森)采样
    知识图谱简介
    显式反馈和隐式反馈
    RNN之LSTM及双向LSTM
  • 原文地址:https://www.cnblogs.com/tk55/p/8580451.html
Copyright © 2011-2022 走看看