多线程阻塞队列缓冲池
2013-04-23 15:02
375 查看
class BoundedBuffer { final Lock lock = new ReentrantLock(); final Condition notFull = lock.newCondition(); final Condition notEmpty = lock.newCondition(); final Object[] items = new Object[100]; int putptr, takeptr, count; public void put(Object x) throws InterruptedException { lock.lock(); try { while (count == items.length) notFull.await(); items[putptr] = x; if (++putptr == items.length) putptr = 0; ++count; notEmpty.signal(); } finally { lock.unlock(); } } public Object take() throws InterruptedException { lock.lock(); try { while (count == 0) notEmpty.await(); Object x = items[takeptr]; if (++takeptr == items.length) takeptr = 0; --count; notFull.signal(); return x; } finally { lock.unlock(); } } }
相关文章推荐
- 多线程阻塞队列缓冲池
- 多线程中的BlockingQueue(阻塞队列)
- java多线程 --ConcurrentLinkedQueue 非阻塞 线程安全队列
- 多线程Demo-ArrayBlockingQueue阻塞队列
- 多线程 阻塞队列
- 阻塞队列Qeue在take的状态下推出多线程循环(笔记)
- (八) Java多线程详解之阻塞队列BlockingQueue及队列优先级详解
- Java核心知识点学习----多线程中的阻塞队列,ArrayBlockingQueue介绍
- 【多线程】 java线程实例(测试阻塞队列&&线程池)
- java多线程:15、阻塞队列【BlockingQueue】
- Java5 多线程(八)-- ArrayBlockingQueue阻塞队列
- java 多线程 阻塞队列
- [疯狂Java]多线程:线程通信(监控器信号、Condition信号、阻塞队列)
- Java多线程——阻塞队列和并发集合
- Java多线程——4 阻塞队列
- Java多线程 阻塞队列和并发集合
- Java5 多线程(八)-- ArrayBlockingQueue阻塞队列
- java多线程-专题-聊聊并发(七)——Java中的阻塞队列
- Java多线程 阻塞队列和并发集合
- java 多线程之-阻塞队列