Search in sources :

Example 6 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project Signal-Android by WhisperSystems.

the class LinkedBlockingDeque method pollLast.

public E pollLast() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        return unlinkLast();
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 7 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project Signal-Android by WhisperSystems.

the class LinkedBlockingDeque method toString.

@Override
public String toString() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        Node<E> p = first;
        if (p == null)
            return "[]";
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (; ; ) {
            E e = p.item;
            sb.append(e == this ? "(this Collection)" : e);
            p = p.next;
            if (p == null)
                return sb.append(']').toString();
            sb.append(',').append(' ');
        }
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 8 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project Signal-Android by WhisperSystems.

the class LinkedBlockingDeque method putFirst.

/**
     * @throws NullPointerException {@inheritDoc}
     * @throws InterruptedException {@inheritDoc}
     */
public void putFirst(E e) throws InterruptedException {
    if (e == null)
        throw new NullPointerException();
    Node<E> node = new Node<E>(e);
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        while (!linkFirst(node)) notFull.await();
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 9 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project Signal-Android by WhisperSystems.

the class LinkedBlockingDeque method toArray.

/**
     * Returns an array containing all of the elements in this deque, in
     * proper sequence; the runtime type of the returned array is that of
     * the specified array.  If the deque fits in the specified array, it
     * is returned therein.  Otherwise, a new array is allocated with the
     * runtime type of the specified array and the size of this deque.
     *
     * <p>If this deque fits in the specified array with room to spare
     * (i.e., the array has more elements than this deque), the element in
     * the array immediately following the end of the deque is set to
     * {@code null}.
     *
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
     * array-based and collection-based APIs.  Further, this method allows
     * precise control over the runtime type of the output array, and may,
     * under certain circumstances, be used to save allocation costs.
     *
     * <p>Suppose {@code x} is a deque known to contain only strings.
     * The following code can be used to dump the deque into a newly
     * allocated array of {@code String}:
     *
     * <pre>
     *     String[] y = x.toArray(new String[0]);</pre>
     *
     * Note that {@code toArray(new Object[0])} is identical in function to
     * {@code toArray()}.
     *
     * @param a the array into which the elements of the deque are to
     *          be stored, if it is big enough; otherwise, a new array of the
     *          same runtime type is allocated for this purpose
     * @return an array containing all of the elements in this deque
     * @throws ArrayStoreException if the runtime type of the specified array
     *         is not a supertype of the runtime type of every element in
     *         this deque
     * @throws NullPointerException if the specified array is null
     */
@Override
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        if (a.length < count)
            a = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), count);
        int k = 0;
        for (Node<E> p = first; p != null; p = p.next) a[k++] = (T) p.item;
        if (a.length > k)
            a[k] = null;
        return a;
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 10 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project Signal-Android by WhisperSystems.

the class LinkedBlockingDeque method toArray.

/*
     * TODO: Add support for more efficient bulk operations.
     *
     * We don't want to acquire the lock for every iteration, but we
     * also want other threads a chance to interact with the
     * collection, especially when count is close to capacity.
     */
//     /**
//      * Adds all of the elements in the specified collection to this
//      * queue.  Attempts to addAll of a queue to itself result in
//      * {@code IllegalArgumentException}. Further, the behavior of
//      * this operation is undefined if the specified collection is
//      * modified while the operation is in progress.
//      *
//      * @param c collection containing elements to be added to this queue
//      * @return {@code true} if this queue changed as a result of the call
//      * @throws ClassCastException            {@inheritDoc}
//      * @throws NullPointerException          {@inheritDoc}
//      * @throws IllegalArgumentException      {@inheritDoc}
//      * @throws IllegalStateException         {@inheritDoc}
//      * @see #add(Object)
//      */
//     public boolean addAll(Collection<? extends E> c) {
//         if (c == null)
//             throw new NullPointerException();
//         if (c == this)
//             throw new IllegalArgumentException();
//         final ReentrantLock lock = this.lock;
//         lock.lock();
//         try {
//             boolean modified = false;
//             for (E e : c)
//                 if (linkLast(e))
//                     modified = true;
//             return modified;
//         } finally {
//             lock.unlock();
//         }
//     }
/**
     * Returns an array containing all of the elements in this deque, in
     * proper sequence (from first to last element).
     *
     * <p>The returned array will be "safe" in that no references to it are
     * maintained by this deque.  (In other words, this method must allocate
     * a new array).  The caller is thus free to modify the returned array.
     *
     * <p>This method acts as bridge between array-based and collection-based
     * APIs.
     *
     * @return an array containing all of the elements in this deque
     */
@Override
@SuppressWarnings("unchecked")
public Object[] toArray() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        Object[] a = new Object[count];
        int k = 0;
        for (Node<E> p = first; p != null; p = p.next) a[k++] = p.item;
        return a;
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Aggregations

ReentrantLock (java.util.concurrent.locks.ReentrantLock)1524 Lock (java.util.concurrent.locks.Lock)125 Test (org.junit.Test)78 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)74 Condition (java.util.concurrent.locks.Condition)57 ArrayList (java.util.ArrayList)45 IOException (java.io.IOException)42 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)27 File (java.io.File)22 TimeoutException (java.util.concurrent.TimeoutException)19 Before (org.junit.Before)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)17 HashMap (java.util.HashMap)16 CountDownLatch (java.util.concurrent.CountDownLatch)16 ExecutionException (java.util.concurrent.ExecutionException)15 List (java.util.List)14 AtomicLong (java.util.concurrent.atomic.AtomicLong)14 Map (java.util.Map)11 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)11 Pair (android.util.Pair)10