自己实现阻塞队列
来源:互联网 发布:苹果软件发布 编辑:程序博客网 时间:2024/05/17 01:14
照着ArrayBlockingQueue自己实现了一些阻塞队列,其原理就是生产者消费者关系,先实现了功能,一些细节(为什么不用wite,notify 为什么不用lock.lock())暂时没有去学习。
队列代码:
import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;/** * Created on 2017/8/31. */public class MyBlockingQueue { final Object[] items=new Object[3]; final ReentrantLock lock=new ReentrantLock(); private final Condition notFull; private static int num=-1; public MyBlockingQueue() { this.notFull = lock.newCondition(); } public void put(Object item) throws InterruptedException {// 如果满了则开始阻塞 lock.lockInterruptibly(); try { while(items.length==num+1){ System.out.println("queue is filled and begin to wite"); notFull.await(); } num++; items[num]=item; System.out.println("put .."+item); System.out.println("put success and try to notify"); notFull.signal(); }finally { lock.unlock(); } } public Object take(int index) throws InterruptedException { if (items.length<index){ System.out.println("index is overflow...index="+index+",length="+items.length); return null; }// 为空则阻塞 lock.lockInterruptibly(); try { while(num==-1){ System.out.println("queue is empty and begin to wite"); notFull.await(); } Object result=items[num]; items[num]=null; System.out.println("queue`s "+num+"is already remove"); num--; System.out.println("try to notify"); notFull.signal(); return result; }finally { lock.unlock(); } }}
测试类代码:
/** * Created on 2017/8/31. */public class Mytest { public static void main(String[] args) { final MyBlockingQueue queue=new MyBlockingQueue(); new Thread(new Runnable() { @Override public void run() { for (int i = 0; i <10 ; i++) { try { queue.put(i); Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); new Thread(new Runnable() { @Override public void run() { for (int i = 0; i <10 ; i++) { try { queue.take(0); Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); }}
打印结果:
put ..0put success and try to notifyqueue`s 0is already removetry to notifyput ..1put success and try to notifyput ..2put success and try to notifyput ..3put success and try to notifyqueue is filled and begin to witequeue`s 2is already removetry to notifyput ..4put success and try to notifyqueue is filled and begin to witequeue`s 2is already removetry to notifyput ..5put success and try to notifyqueue is filled and begin to witequeue`s 2is already removetry to notifyput ..6put success and try to notifyqueue is filled and begin to witequeue`s 2is already removetry to notifyput ..7put success and try to notifyqueue is filled and begin to witequeue`s 2is already removetry to notifyput ..8put success and try to notifyqueue is filled and begin to witequeue`s 2is already removetry to notifyput ..9put success and try to notifyqueue`s 2is already removetry to notifyqueue`s 1is already removetry to notifyqueue`s 0is already removetry to notify
阅读全文
0 0
- 自己实现阻塞队列
- 通过Condition实现自己的阻塞队列
- Java5实现阻塞队列
- 实现阻塞队列
- C++阻塞队列实现
- Java5实现阻塞队列
- Java 阻塞队列实现
- java 之 阻塞队列实现
- Java阻塞队列的实现
- Java阻塞队列的实现
- Java阻塞队列的实现
- Java阻塞队列的实现
- Java阻塞队列的实现
- c++ 阻塞队列的实现
- Java阻塞队列的实现
- java 阻塞队列自定义实现
- 阻塞队列实现生产者消费者
- linkedBlockingQueue实现事件阻塞队列
- kafka性能参数和压力测试揭秘
- C#开发常见的异常
- iframe-页面展示PDF
- 刘晓庆8段感情都传奇,当过小三为姜文掏心掏肺现得富商老公进军微电商!
- 深度优先搜索DFS和广度优先搜索BFS的总结
- 自己实现阻塞队列
- 数据结构中常用排序
- cookie 原理及应用
- hibernate 多对一关联关系报错
- 手机编程经验总结
- top命令标识详解
- android中一个app跳转到另一个app中(超级详细)
- 从源码出发浅析Android TV的焦点移动原理-下篇
- Tensorflow12-Dlib人脸检测