多线程阻塞队列缓冲池
2013-04-23 20:20
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(); } } }
相关文章推荐
- 多线程阻塞队列缓冲池
- 多线程之阻塞队列ArrayBlockingQueue,BlockingQueue
- java多线程之线程并发库阻塞队列的应用
- Java多线程 阻塞队列和并发集合
- Java多线程 阻塞队列和并发集合
- 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法 在Java多线程应用中,队列的使用率很高,多数生产消费模型的首选数据结构就是队列(先进先出)。
- Java多线程之阻塞队列
- Java多线程 阻塞队列和并发集合
- c++ 多线程阻塞队列的简单实现
- Java多线程:队列与阻塞队列
- java多线程-03-阻塞队列简介
- 多线程之阻塞队列
- [C++ 2011 多线程系列二]多线程阻塞队列
- BlockingQueue 阻塞队列 -- Java 多线程
- 多线程之9——BlockingQueue (阻塞队列)
- java多线程:16、阻塞队列【BlockingQueue】实现线程数据共享的效果
- 一心多用多线程-阻塞队列(3)-DelayQueue
- Java多线程阻塞队列和并发集合
- Java多线程之模拟一个阻塞队列
- 一心多用多线程-阻塞队列(1)-ArrayBlockingQueue