• 阻塞自定义队列


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

    结果

  • 相关阅读:
    使用SSH搭建简易的网上购物系统
    myeclipse8.0中安装maven插件m2eclipse
    Ant入门
    hibernate中多对多分解成一对多,
    在eclipse上,安装myeclipse插件
    hibernate复合主键的映射
    struts2中struts:iterator标签的使用
    hibernate复合主键同时做外键
    Mahout问题总括
    面向对象基础概念
  • 原文地址:https://www.cnblogs.com/tk55/p/8580451.html
Copyright © 2020-2023  润新知