Java Code Examples for java.util.concurrent.locks.ReentrantLock#unlock()

The following examples show how to use java.util.concurrent.locks.ReentrantLock#unlock() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultiLockFairBlockingQueue.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked") // Can't create arrays of generic types
public FairIterator() {
    ArrayList<E> list = new ArrayList<E>(MultiLockFairBlockingQueue.this.size());
    for (int idx=0; idx<LOCK_COUNT; idx++) {
        final ReentrantLock lock = MultiLockFairBlockingQueue.this.locks[idx];
        lock.lock();
        try {
            elements = (E[]) new Object[MultiLockFairBlockingQueue.this.items[idx].size()];
            MultiLockFairBlockingQueue.this.items[idx].toArray(elements);

        } finally {
            lock.unlock();
        }
    }
    index = 0;
    elements = (E[]) new Object[list.size()];
    list.toArray(elements);
}
 
Example 2
Source File: LinkedBlockingDeque.java    From Android-Application-ZJB with Apache License 2.0 6 votes vote down vote up
/**
 * Atomically removes all of the elements from this deque.
 * The deque will be empty after this call returns.
 */
public void clear() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        for (Node<E> f = first; f != null; ) {
            f.item = null;
            Node<E> n = f.next;
            f.prev = null;
            f.next = null;
            f = n;
        }
        first = last = null;
        count = 0;
        notFull.signalAll();
    } finally {
        lock.unlock();
    }
}
 
Example 3
Source File: LinkedBlockingQueue.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a {@code LinkedBlockingQueue} with a capacity of
 * {@link Integer#MAX_VALUE}, initially containing the elements of the
 * given collection,
 * added in traversal order of the collection's iterator.
 *
 * @param c the collection of elements to initially contain
 * @throws NullPointerException if the specified collection or any
 *         of its elements are null
 */
public LinkedBlockingQueue(Collection<? extends E> c) {
    this(Integer.MAX_VALUE);
    final ReentrantLock putLock = this.putLock;
    putLock.lock(); // Never contended, but necessary for visibility
    try {
        int n = 0;
        for (E e : c) {
            if (e == null)
                throw new NullPointerException();
            if (n == capacity)
                throw new IllegalStateException("Queue full");
            enqueue(new Node<E>(e));
            ++n;
        }
        count.set(n);
    } finally {
        putLock.unlock();
    }
}
 
Example 4
Source File: ScheduledThreadPoolExecutor.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public int drainTo(Collection<? super Runnable> c) {
    if (c == null)
        throw new NullPointerException();
    if (c == this)
        throw new IllegalArgumentException();
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        RunnableScheduledFuture<?> first;
        int n = 0;
        while ((first = peekExpired()) != null) {
            c.add(first);   // In this order, in case add() throws.
            finishPoll(first);
            ++n;
        }
        return n;
    } finally {
        lock.unlock();
    }
}
 
Example 5
Source File: BufferPool.java    From binlake with Apache License 2.0 6 votes vote down vote up
public ByteBuffer allocate() {
    ByteBuffer node = null;
    final ReentrantLock lock = this.lock;
    lock.lock();

    try {
        node = (count == 0) ? null : extract();
    } finally {
        lock.unlock();
    }

    if (node == null) {
        ++newCount;
        return create(chunkSize);
    } else {
        return node;
    }
}
 
Example 6
Source File: ScheduledThreadPoolExecutor.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public int drainTo(Collection<? super Runnable> c, int maxElements) {
    if (c == null)
        throw new NullPointerException();
    if (c == this)
        throw new IllegalArgumentException();
    if (maxElements <= 0)
        return 0;
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        RunnableScheduledFuture<?> first;
        int n = 0;
        while (n < maxElements && (first = peekExpired()) != null) {
            c.add(first);   // In this order, in case add() throws.
            finishPoll(first);
            ++n;
        }
        return n;
    } finally {
        lock.unlock();
    }
}
 
Example 7
Source File: LinkedBlockingQueue.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * @throws UnsupportedOperationException {@inheritDoc}
 * @throws ClassCastException            {@inheritDoc}
 * @throws NullPointerException          {@inheritDoc}
 * @throws IllegalArgumentException      {@inheritDoc}
 */
public int drainTo(Collection<? super E> c, int maxElements) {
    Objects.requireNonNull(c);
    if (c == this)
        throw new IllegalArgumentException();
    if (maxElements <= 0)
        return 0;
    boolean signalNotFull = false;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();
    try {
        int n = Math.min(maxElements, count.get());
        // count.get provides visibility to first n Nodes
        Node<E> h = head;
        int i = 0;
        try {
            while (i < n) {
                Node<E> p = h.next;
                c.add(p.item);
                p.item = null;
                h.next = h;
                h = p;
                ++i;
            }
            return n;
        } finally {
            // Restore invariants even if c.add() threw
            if (i > 0) {
                // assert h.item == null;
                head = h;
                signalNotFull = (count.getAndAdd(-i) == capacity);
            }
        }
    } finally {
        takeLock.unlock();
        if (signalNotFull)
            signalNotFull();
    }
}
 
Example 8
Source File: LinkedBlockingDeque.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public E pollLast() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        return unlinkLast();
    } finally {
        lock.unlock();
    }
}
 
Example 9
Source File: LinkedBlockingDeque.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 10
Source File: ScheduledThreadPoolExecutor.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public boolean contains(Object x) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        return indexOf(x) != -1;
    } finally {
        lock.unlock();
    }
}
 
Example 11
Source File: LinkedBlockingQueue.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Signals a waiting take. Called only from put/offer (which do not
 * otherwise ordinarily lock takeLock.)
 */
private void signalNotEmpty() {
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();
    try {
        notEmpty.signal();
    } finally {
        takeLock.unlock();
    }
}
 
Example 12
Source File: LinkedBlockingDeque.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public Spliterator<E> trySplit() {
    Node<E> h;
    final LinkedBlockingDeque<E> q = this.queue;
    int b = batch;
    int n = (b <= 0) ? 1 : (b >= MAX_BATCH) ? MAX_BATCH : b + 1;
    if (!exhausted &&
        ((h = current) != null || (h = q.first) != null) &&
        h.next != null) {
        Object[] a = new Object[n];
        final ReentrantLock lock = q.lock;
        int i = 0;
        Node<E> p = current;
        lock.lock();
        try {
            if (p != null || (p = q.first) != null) {
                do {
                    if ((a[i] = p.item) != null)
                        ++i;
                } while ((p = p.next) != null && i < n);
            }
        } finally {
            lock.unlock();
        }
        if ((current = p) == null) {
            est = 0L;
            exhausted = true;
        }
        else if ((est -= i) < 0L)
            est = 0L;
        if (i > 0) {
            batch = i;
            return Spliterators.spliterator
                (a, 0, i, Spliterator.ORDERED | Spliterator.NONNULL |
                 Spliterator.CONCURRENT);
        }
    }
    return null;
}
 
Example 13
Source File: CopyOnWriteArrayList.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
public ListIterator<E> listIterator(int index) {
    final ReentrantLock lock = l.lock;
    lock.lock();
    try {
        checkForComodification();
        if (index < 0 || index > size)
            throw new IndexOutOfBoundsException("Index: "+index+
                                                ", Size: "+size);
        return new COWSubListIterator<E>(l, index, offset, size);
    } finally {
        lock.unlock();
    }
}
 
Example 14
Source File: ThreadPoolExecutor.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Performs cleanup and bookkeeping for a dying worker. Called
 * only from worker threads. Unless completedAbruptly is set,
 * assumes that workerCount has already been adjusted to account
 * for exit.  This method removes thread from worker set, and
 * possibly terminates the pool or replaces the worker if either
 * it exited due to user task exception or if fewer than
 * corePoolSize workers are running or queue is non-empty but
 * there are no workers.
 *
 * @param w the worker
 * @param completedAbruptly if the worker died due to user exception
 */
private void processWorkerExit(Worker w, boolean completedAbruptly) {
    if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted
        decrementWorkerCount();

    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        completedTaskCount += w.completedTasks;
        workers.remove(w);
    } finally {
        mainLock.unlock();
    }

    tryTerminate();

    int c = ctl.get();
    if (runStateLessThan(c, STOP)) {
        if (!completedAbruptly) {
            int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
            if (min == 0 && ! workQueue.isEmpty())
                min = 1;
            if (workerCountOf(c) >= min)
                return; // replacement not needed
        }
        addWorker(null, false);
    }
}
 
Example 15
Source File: LinkedBlockingDeque.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
AbstractItr() {
    // set to initial position
    final ReentrantLock lock = LinkedBlockingDeque.this.lock;
    lock.lock();
    try {
        if ((next = firstNode()) != null)
            nextItem = next.item;
    } finally {
        lock.unlock();
    }
}
 
Example 16
Source File: ArrayBlockingQueue.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the number of elements in this queue.
 *
 * @return the number of elements in this queue
 */
public int size() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        return count;
    } finally {
        lock.unlock();
    }
}
 
Example 17
Source File: DelayQueue.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Atomically removes all of the elements from this delay queue.
 * The queue will be empty after this call returns.
 * Elements with an unexpired delay are not waited for; they are
 * simply discarded from the queue.
 */
public void clear() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        q.clear();
    } finally {
        lock.unlock();
    }
}
 
Example 18
Source File: CopyOnWriteArrayList.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * A version of remove(Object) using the strong hint that given
 * recent snapshot contains o at the given index.
 */
private boolean remove(Object o, Object[] snapshot, int index) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        Object[] current = getArray();
        int len = current.length;
        if (snapshot != current) findIndex: {
            int prefix = Math.min(index, len);
            for (int i = 0; i < prefix; i++) {
                if (current[i] != snapshot[i] && eq(o, current[i])) {
                    index = i;
                    break findIndex;
                }
            }
            if (index >= len)
                return false;
            if (current[index] == o)
                break findIndex;
            index = indexOf(o, current, index, len);
            if (index < 0)
                return false;
        }
        Object[] newElements = new Object[len - 1];
        System.arraycopy(current, 0, newElements, 0, index);
        System.arraycopy(current, index + 1,
                         newElements, index,
                         len - index - 1);
        setArray(newElements);
        return true;
    } finally {
        lock.unlock();
    }
}
 
Example 19
Source File: CopyOnWriteArrayList.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
public E get(int index) {
    final ReentrantLock lock = l.lock;
    lock.lock();
    try {
        rangeCheck(index);
        checkForComodification();
        return l.get(index+offset);
    } finally {
        lock.unlock();
    }
}
 
Example 20
Source File: LinkedBlockingDeque.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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> {@code 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
 */
@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();
    }
}