Package java.util.concurrent.locks

Examples of java.util.concurrent.locks.ReentrantLock


    public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
        Importance i = (Importance) e;
        InternalQueue<E> internalQueue = getQueueForPriority(i.getPriority());

        long nanos = unit.toNanos(timeout);
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            for (;;) {
                if (internalQueue.remainingCapacity() > 0) {
                    internalQueue.offer(e);
                    count++;
                    notEmpty.signal();
                    return true;
                }
                if (nanos <= 0)
                    return false;
                try {
                    nanos = internalQueue.getNotFullCond().awaitNanos(nanos);
                } catch (InterruptedException ie) {
                    internalQueue.getNotFullCond().signal();
                    throw ie;
                }
            }
        } finally {
            lock.unlock();
        }
    }
View Full Code Here


     * Get an element. Block until an element is available
     * @return an element
     * @throws InterruptedException if the thread is interrupted
     */
    public E take() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            InternalQueue<E> internalQueue = nextQueueAlgorithm.getNextQueue();
            try {
                while (internalQueue == null) {
                    notEmpty.await();
                    internalQueue = nextQueueAlgorithm.getNextQueue();
                }
            } catch (InterruptedException ie) {
                notEmpty.signal();
                throw ie;
            }
            E e = internalQueue.poll();
            count--;
            internalQueue.getNotFullCond().signal();
            return e;
        } finally {
            lock.unlock();
        }
    }
View Full Code Here

     * @return an object
     * @throws InterruptedException
     */
    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
        long nanos = unit.toNanos(timeout);
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            for (;;) {
                InternalQueue<E> internalQueue = nextQueueAlgorithm.getNextQueue();
                if (internalQueue != null) {
                    E e = internalQueue.poll();
                    count--;
                    internalQueue.getNotFullCond().signal();
                    return e;
                }
                if (nanos <= 0)
                    return null;
                try {
                    nanos = notEmpty.awaitNanos(nanos);
                } catch (InterruptedException ie) {
                    notEmpty.signal();
                    throw ie;
                }
            }
        } finally {
            lock.unlock();
        }
    }
View Full Code Here

     * @param c collection to drain the items    
     * @return number of elements copied
     */
    public int drainTo(Collection<? super E> c) {
        int count = 0;
        final ReentrantLock lock = this.lock;
        lock.lock();

        try {
            for (InternalQueue<E> internalQueue : queues) {
                count += internalQueue.drainTo(c);
            }
            this.count = this.count - count;
        } finally {
            lock.unlock();
        }
        return count;
    }
View Full Code Here

     * @param maxElements maximum elements to copy
     * @return number of elements copied
     */
    public int drainTo(Collection<? super E> c, int maxElements) {
        int elementsCopied = 0;
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            for (InternalQueue<E> internalQueue : queues) {
                elementsCopied += internalQueue.drainTo(c,
                        internalQueue.size() > (maxElements - elementsCopied) ?
                                (maxElements - elementsCopied) : internalQueue.size());
            }
            count = count - elementsCopied;
        } finally {
            lock.unlock();
        }
        return elementsCopied;
    }
View Full Code Here

     * Block indefinitely until a object is available for retrieval.
     *
     * @return an object
     */
    public E poll() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            InternalQueue<E> internalQueue = nextQueueAlgorithm.getNextQueue();
            if (internalQueue != null) {
                count--;
                E e = internalQueue.poll();
                internalQueue.getNotFullCond().signal();
                return e;
            } else {
                return null;
            }
        } finally {
            lock.unlock();
        }
    }
View Full Code Here

    public int remainingCapacity() {
        return capacity - count;
    }

    public E peek() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            InternalQueue<E> internalQueue = nextQueueAlgorithm.getNextQueue();
            if (internalQueue != null) {
                return internalQueue.peek();
            } else {
                return null;
            }
        } finally {
            lock.unlock();
        }
    }
View Full Code Here

    public boolean isEmpty() {
        return count == 0;
    }

    public boolean remove(Object o) {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            for (InternalQueue<E> internalQueue : queues) {
                if (internalQueue.remove(o)) {
                    count--;
                    return true;
                }
            }
            return false;
        } finally {
            lock.unlock();
        }
    }
View Full Code Here

            lock.unlock();
        }
    }

    public boolean contains(Object o) {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            for (InternalQueue<E> internalQueue : queues) {
                if (internalQueue.contains(o)) return true;
            }
            return false;
        } finally {
            lock.unlock();
        }
    }
View Full Code Here

            lock.unlock();
        }
    }

    public String toString() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            StringBuffer s = new StringBuffer();
            for (InternalQueue<E> internalQueue : queues) {
                s.append(internalQueue.toString());
            }
            return s.toString();
        } finally {
            lock.unlock();
        }
    }
View Full Code Here

TOP

Related Classes of java.util.concurrent.locks.ReentrantLock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.