博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
阻塞自定义队列
阅读量:7116 次
发布时间:2019-06-28

本文共 1516 字,大约阅读时间需要 5 分钟。

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

结果

 

转载地址:http://quwel.baihongyu.com/

你可能感兴趣的文章
map侧连接
查看>>
数据库---数据库查询的各种子句
查看>>
vue+Mint-ui实现登录注册
查看>>
asp.net记住我功能
查看>>
[java web]Idea+maven+spring4+hibernate5+struts2整合过程
查看>>
Mybatis多参数
查看>>
[LibreOJ #2341]【WC2018】即时战略【交互】【LCT】
查看>>
动画--easeljs中的movieClip控件示例?
查看>>
细说浏览器特性检测(1)-jQuery1.4添加部分
查看>>
C errno是否是线程安全的
查看>>
类的初始化
查看>>
百度AI开放平台 UNIT平台开发在线客服 借助百度的人工智能如何开发一个在线客服系统...
查看>>
python大战机器学习——半监督学习
查看>>
ethereum/EIPs-1271 smart contract
查看>>
ADempiere3.6.0LTS - 重新导入会计科目(基于Ubuntu Desktop 12.04 LTS)
查看>>
Project Euler Problem 48: Self powers
查看>>
python一个小程序:猜数字
查看>>
转:web.xml 配置中classpath: 与classpath*:的区别
查看>>
vue-自定义组件传
查看>>
由用户反映DroidPilot安装之后,License没有同步安装 - 解决办法
查看>>