java.util.concurrent.locks.ReentrantLock#unlock()源码实例Demo

下面列出了java.util.concurrent.locks.ReentrantLock#unlock() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Tomcat7.0.67   文件: MultiLockFairBlockingQueue.java
@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);
}
 
源代码2 项目: jdk8u-jdk   文件: ScheduledThreadPoolExecutor.java
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();
    }
}
 
源代码3 项目: binlake   文件: BufferPool.java
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;
    }
}
 
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();
    }
}
 
源代码5 项目: database   文件: LinkedBlockingQueue.java
/**
 * 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();
    }
}
 
/**
 * 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();
    }
}
 
源代码7 项目: jdk8u_jdk   文件: LinkedBlockingDeque.java
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;
}
 
源代码8 项目: JDKSourceCode1.8   文件: CopyOnWriteArrayList.java
public E get(int index) {
    final ReentrantLock lock = l.lock;
    lock.lock();
    try {
        rangeCheck(index);
        checkForComodification();
        return l.get(index+offset);
    } finally {
        lock.unlock();
    }
}
 
源代码9 项目: hottub   文件: CopyOnWriteArrayList.java
/**
 * 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();
    }
}
 
源代码10 项目: jdk8u-jdk   文件: DelayQueue.java
/**
 * 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();
    }
}
 
源代码11 项目: Java8CN   文件: ArrayBlockingQueue.java
/**
 * 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();
    }
}
 
源代码12 项目: Bytecoder   文件: LinkedBlockingDeque.java
AbstractItr() {
    // set to initial position
    final ReentrantLock lock = LinkedBlockingDeque.this.lock;
    lock.lock();
    try {
        if ((next = firstNode()) != null)
            nextItem = next.item;
    } finally {
        lock.unlock();
    }
}
 
源代码13 项目: jdk8u_jdk   文件: ThreadPoolExecutor.java
/**
 * 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);
    }
}
 
源代码14 项目: JDKSourceCode1.8   文件: CopyOnWriteArrayList.java
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();
    }
}
 
源代码15 项目: Bytecoder   文件: LinkedBlockingQueue.java
/**
 * @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();
    }
}
 
源代码16 项目: openjdk-jdk9   文件: LinkedBlockingQueue.java
/**
 * 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();
    }
}
 
public boolean contains(Object x) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        return indexOf(x) != -1;
    } finally {
        lock.unlock();
    }
}
 
源代码18 项目: openjdk-8-source   文件: LinkedBlockingDeque.java
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();
    }
}
 
源代码19 项目: jdk8u_jdk   文件: LinkedBlockingDeque.java
public E pollLast() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        return unlinkLast();
    } finally {
        lock.unlock();
    }
}
 
源代码20 项目: openjdk-8   文件: LinkedBlockingDeque.java
/**
 * 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();
    }
}