Search in sources :

Example 31 with ReentrantLock

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

the class LinkedBlockingDeque method writeObject.

/**
     * Save the state of this deque to a stream (that is, serialize it).
     *
     * @serialData The capacity (int), followed by elements (each an
     * {@code Object}) in the proper order, followed by a null
     * @param s the stream
     */
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        // Write out capacity and any hidden stuff
        s.defaultWriteObject();
        // Write out all elements in the proper order.
        for (Node<E> p = first; p != null; p = p.next) s.writeObject(p.item);
        // Use trailing null as sentinel
        s.writeObject(null);
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 32 with ReentrantLock

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

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

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

the class LinkedBlockingDeque method pollLast.

public E pollLast(long timeout, TimeUnit unit) throws InterruptedException {
    long nanos = unit.toNanos(timeout);
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
        E x;
        while ((x = unlinkLast()) == null) {
            if (nanos <= 0)
                return null;
            nanos = notEmpty.awaitNanos(nanos);
        }
        return x;
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 34 with ReentrantLock

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

the class LinkedBlockingDeque method contains.

/**
     * Returns {@code true} if this deque contains the specified element.
     * More formally, returns {@code true} if and only if this deque contains
     * at least one element {@code e} such that {@code o.equals(e)}.
     *
     * @param o object to be checked for containment in this deque
     * @return {@code true} if this deque contains the specified element
     */
public boolean contains(Object o) {
    if (o == null)
        return false;
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        for (Node<E> p = first; p != null; p = p.next) if (o.equals(p.item))
            return true;
        return false;
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 35 with ReentrantLock

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

the class LinkedBlockingQueue method put.

/**
     * Inserts the specified element at the tail of this queue, waiting if
     * necessary for space to become available.
     *
     * @throws InterruptedException {@inheritDoc}
     * @throws NullPointerException {@inheritDoc}
     */
public void put(E e) throws InterruptedException {
    if (e == null)
        throw new NullPointerException();
    // Note: convention in all put/take/etc is to preset local var
    // holding count negative to indicate failure unless set.
    int c = -1;
    Node<E> node = new Node(e);
    final ReentrantLock putLock = this.putLock;
    final AtomicInteger count = this.count;
    putLock.lockInterruptibly();
    try {
        /*
             * Note that count is used in wait guard even though it is
             * not protected by lock. This works because count can
             * only decrease at this point (all other puts are shut
             * out by lock), and we (or some other waiting put) are
             * signalled if it ever changes from capacity. Similarly
             * for all other uses of count in other wait guards.
             */
        while (count.get() == capacity) {
            notFull.await();
        }
        enqueue(node);
        c = count.getAndIncrement();
        if (c + 1 < capacity)
            notFull.signal();
    } finally {
        putLock.unlock();
    }
    if (c == 0)
        signalNotEmpty();
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

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