Search in sources :

Example 26 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project XobotOS by xamarin.

the class LinkedBlockingDeque method takeFirst.

public E takeFirst() throws InterruptedException {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        E x;
        while ((x = unlinkFirst()) == null) notEmpty.await();
        return x;
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 27 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project XobotOS by xamarin.

the class LinkedBlockingDeque method toString.

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 28 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project XobotOS by xamarin.

the class LinkedBlockingDeque method pollFirst.

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

Example 29 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project XobotOS by xamarin.

the class LinkedBlockingDeque method offerFirst.

/**
     * @throws NullPointerException {@inheritDoc}
     */
public boolean offerFirst(E e) {
    if (e == null)
        throw new NullPointerException();
    Node<E> node = new Node<E>(e);
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        return linkFirst(node);
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 30 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project XobotOS by xamarin.

the class LinkedBlockingDeque method drainTo.

/**
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws ClassCastException            {@inheritDoc}
     * @throws NullPointerException          {@inheritDoc}
     * @throws IllegalArgumentException      {@inheritDoc}
     */
public int drainTo(Collection<? super E> c, int maxElements) {
    if (c == null)
        throw new NullPointerException();
    if (c == this)
        throw new IllegalArgumentException();
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        int n = Math.min(maxElements, count);
        for (int i = 0; i < n; i++) {
            // In this order, in case add() throws.
            c.add(first.item);
            unlinkFirst();
        }
        return n;
    } 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