Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BlockingQueue<T> {
- private Queue<T> queue = new LinkedList<T>();
- private int capacity;
- private Lock lock = new ReentrantLock();
- private Condition notFull = lock.newCondition();
- private Condition notEmpty = lock.newCondition();
- public BlockingQueue(int capacity) {
- this.capacity = capacity;
- }
- public void put(T element) throws InterruptedException {
- lock.lock();
- try {
- while(queue.size() == capacity) {
- notFull.await();
- }
- queue.add(element);
- notEmpty.signal();
- } finally {
- lock.unlock();
- }
- }
- public T take() throws InterruptedException {
- lock.lock();
- try {
- while(queue.isEmpty()) {
- notEmpty.await();
- }
- T item = queue.remove();
- notFull.signal();
- return item;
- } finally {
- lock.unlock();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement