Search in sources :

Example 96 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project robovm by robovm.

the class ForkJoinTask method getThrowableException.

/**
     * Returns a rethrowable exception for the given task, if
     * available. To provide accurate stack traces, if the exception
     * was not thrown by the current thread, we try to create a new
     * exception of the same type as the one thrown, but with the
     * recorded exception as its cause. If there is no such
     * constructor, we instead try to use a no-arg constructor,
     * followed by initCause, to the same effect. If none of these
     * apply, or any fail due to other exceptions, we return the
     * recorded exception, which is still correct, although it may
     * contain a misleading stack trace.
     *
     * @return the exception, or null if none
     */
private Throwable getThrowableException() {
    if ((status & DONE_MASK) != EXCEPTIONAL)
        return null;
    int h = System.identityHashCode(this);
    ExceptionNode e;
    final ReentrantLock lock = exceptionTableLock;
    lock.lock();
    try {
        expungeStaleExceptions();
        ExceptionNode[] t = exceptionTable;
        e = t[h & (t.length - 1)];
        while (e != null && e.get() != this) e = e.next;
    } finally {
        lock.unlock();
    }
    Throwable ex;
    if (e == null || (ex = e.ex) == null)
        return null;
    if (false && e.thrower != Thread.currentThread().getId()) {
        Class<? extends Throwable> ec = ex.getClass();
        try {
            Constructor<?> noArgCtor = null;
            // public ctors only
            Constructor<?>[] cs = ec.getConstructors();
            for (int i = 0; i < cs.length; ++i) {
                Constructor<?> c = cs[i];
                Class<?>[] ps = c.getParameterTypes();
                if (ps.length == 0)
                    noArgCtor = c;
                else if (ps.length == 1 && ps[0] == Throwable.class)
                    return (Throwable) (c.newInstance(ex));
            }
            if (noArgCtor != null) {
                Throwable wx = (Throwable) (noArgCtor.newInstance());
                wx.initCause(ex);
                return wx;
            }
        } catch (Exception ignore) {
        }
    }
    return ex;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Constructor(java.lang.reflect.Constructor) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 97 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project robovm by robovm.

the class LinkedBlockingDeque method size.

/**
     * Returns the number of elements in this deque.
     *
     * @return the number of elements in this deque
     */
public int size() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        return count;
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 98 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project robovm by robovm.

the class LinkedBlockingDeque method offerFirst.

/**
     * @throws NullPointerException {@inheritDoc}
     * @throws InterruptedException {@inheritDoc}
     */
public boolean offerFirst(E e, long timeout, TimeUnit unit) throws InterruptedException {
    if (e == null)
        throw new NullPointerException();
    Node<E> node = new Node<E>(e);
    long nanos = unit.toNanos(timeout);
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
        while (!linkFirst(node)) {
            if (nanos <= 0)
                return false;
            nanos = notFull.awaitNanos(nanos);
        }
        return true;
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 99 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project robovm by robovm.

the class PriorityBlockingQueue method remove.

/**
     * Removes a single instance of the specified element from this queue,
     * if it is present.  More formally, removes an element {@code e} such
     * that {@code o.equals(e)}, if this queue contains one or more such
     * elements.  Returns {@code true} if and only if this queue contained
     * the specified element (or equivalently, if this queue changed as a
     * result of the call).
     *
     * @param o element to be removed from this queue, if present
     * @return {@code true} if this queue changed as a result of the call
     */
public boolean remove(Object o) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        int i = indexOf(o);
        if (i == -1)
            return false;
        removeAt(i);
        return true;
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 100 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project robovm by robovm.

the class PriorityBlockingQueue method toString.

public String toString() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        int n = size;
        if (n == 0)
            return "[]";
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < n; ++i) {
            Object e = queue[i];
            sb.append(e == this ? "(this Collection)" : e);
            if (i != n - 1)
                sb.append(',').append(' ');
        }
        return sb.append(']').toString();
    } 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