java.util.concurrent.locks.ReentrantLock Java Examples
The following examples show how to use
java.util.concurrent.locks.ReentrantLock.
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 Project: openjdk-8 Author: bpupadhyaya File: LinkedBlockingDeque.java License: GNU General Public License v2.0 | 6 votes |
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(); } }
Example #2
Source Project: Mycat2 Author: MyCATApache File: PostgreSQLHeartbeat.java License: GNU General Public License v3.0 | 6 votes |
@Override public void stop() { final ReentrantLock lock = this.lock; lock.lock(); try { if (isStop.compareAndSet(false, true)) { if (isChecking.get()) { // nothing } else { PostgreSQLDetector detector = this.detector; if (detector != null) { detector.quit(); isChecking.set(false); } } } } finally { lock.unlock(); } }
Example #3
Source Project: wildfly-core Author: wildfly File: ConcreteResourceRegistration.java License: GNU Lesser General Public License v2.1 | 6 votes |
/** Constructor for a non-root MRR */ ConcreteResourceRegistration(final String valueString, final NodeSubregistry parent, final ResourceDefinition definition, final AccessConstraintUtilizationRegistry constraintUtilizationRegistry, final boolean ordered, CapabilityRegistry capabilityRegistry) { super(valueString, parent); this.constraintUtilizationRegistry = constraintUtilizationRegistry; this.capabilityRegistry = capabilityRegistry; this.resourceDefinition = definition; // If our parent is runtime-only, so are we, otherwise follow the definition this.runtimeOnly = parent.isRuntimeOnly() || definition.isRuntime(); this.accessConstraintDefinitions = buildAccessConstraints(); this.ordered = ordered; // For non-root MRRs we don't expect much in the way of concurrent reads in performance // critical situations, so we want lock/unlock to be as simple and fast as possible // So we just use a single non-r/w lock for both reads and writes this.readLock = this.writeLock = new ReentrantLock(); }
Example #4
Source Project: jdk8u-jdk Author: lambdalab-mirror File: ForkJoinTask.java License: GNU General Public License v2.0 | 6 votes |
/** * If lock is available, poll stale refs and remove them. * Called from ForkJoinPool when pools become quiescent. */ static final void helpExpungeStaleExceptions() { final ReentrantLock lock = exceptionTableLock; if (lock.tryLock()) { try { expungeStaleExceptions(); } finally { lock.unlock(); } } }
Example #5
Source Project: jdk8u-jdk Author: lambdalab-mirror File: LinkedBlockingDeque.java License: GNU General Public License v2.0 | 6 votes |
public boolean removeFirstOccurrence(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)) { unlink(p); return true; } } return false; } finally { lock.unlock(); } }
Example #6
Source Project: j2objc Author: google File: ArrayBlockingQueue.java License: Apache License 2.0 | 6 votes |
/** * Atomically removes all of the elements from this queue. * The queue will be empty after this call returns. */ public void clear() { final ReentrantLock lock = this.lock; lock.lock(); try { int k = count; if (k > 0) { final Object[] items = this.items; final int putIndex = this.putIndex; int i = takeIndex; do { items[i] = null; if (++i == items.length) i = 0; } while (i != putIndex); takeIndex = putIndex; count = 0; if (itrs != null) itrs.queueIsEmpty(); for (; k > 0 && lock.hasWaiters(notFull); k--) notFull.signal(); } } finally { lock.unlock(); } }
Example #7
Source Project: jdk8u-dev-jdk Author: frohoff File: ArrayBlockingQueue.java License: GNU General Public License v2.0 | 6 votes |
/** * Returns {@code true} if this queue contains the specified element. * More formally, returns {@code true} if and only if this queue contains * at least one element {@code e} such that {@code o.equals(e)}. * * @param o object to be checked for containment in this queue * @return {@code true} if this queue contains the specified element */ public boolean contains(Object o) { if (o == null) return false; final Object[] items = this.items; final ReentrantLock lock = this.lock; lock.lock(); try { if (count > 0) { final int putIndex = this.putIndex; int i = takeIndex; do { if (o.equals(items[i])) return true; if (++i == items.length) i = 0; } while (i != putIndex); } return false; } finally { lock.unlock(); } }
Example #8
Source Project: dragonwell8_jdk Author: alibaba File: ThreadPoolExecutor.java License: GNU General Public License v2.0 | 6 votes |
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { for (;;) { if (runStateAtLeast(ctl.get(), TERMINATED)) return true; if (nanos <= 0) return false; nanos = termination.awaitNanos(nanos); } } finally { mainLock.unlock(); } }
Example #9
Source Project: linstor-server Author: LINBIT File: EventBroker.java License: GNU General Public License v3.0 | 6 votes |
@Inject public EventBroker( ErrorReporter errorReporterRef, CommonSerializer commonSerializerRef, WatchStore watchStoreRef, Map<String, EventSerializer> eventSerializersRef, Map<String, EventSerializerDescriptor> eventSerializerDescriptorsRef ) { errorReporter = errorReporterRef; commonSerializer = commonSerializerRef; watchStore = watchStoreRef; eventSerializers = eventSerializersRef; eventSerializerDescriptors = eventSerializerDescriptorsRef; watchLock = new ReentrantLock(); }
Example #10
Source Project: WliveTV Author: wanliyang1990 File: LinkedBlockingDeque.java License: Apache License 2.0 | 6 votes |
/** * @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(); } }
Example #11
Source Project: android_9.0.0_r45 Author: lulululbj File: CaptureCollector.java License: Apache License 2.0 | 6 votes |
/** * Called to alert the {@link CaptureCollector} that the jpeg capture has begun. * * @param timestamp the time of the jpeg capture. * @return the {@link RequestHolder} for the request associated with this capture. */ public RequestHolder jpegCaptured(long timestamp) { final ReentrantLock lock = this.mLock; lock.lock(); try { CaptureHolder h = mJpegCaptureQueue.poll(); if (h == null) { Log.w(TAG, "jpegCaptured called with no jpeg request on queue!"); return null; } h.setJpegTimestamp(timestamp); return h.mRequest; } finally { lock.unlock(); } }
Example #12
Source Project: openjdk-8-source Author: keerath File: ThreadPoolExecutor.java License: GNU General Public License v2.0 | 6 votes |
/** * Returns the approximate total number of tasks that have ever been * scheduled for execution. Because the states of tasks and * threads may change dynamically during computation, the returned * value is only an approximation. * * @return the number of tasks */ public long getTaskCount() { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { long n = completedTaskCount; for (Worker w : workers) { n += w.completedTasks; if (w.isLocked()) ++n; } return n + workQueue.size(); } finally { mainLock.unlock(); } }
Example #13
Source Project: tilt-game-android Author: mediamonks File: PriorityBlockingAggregatorQueue.java License: MIT License | 6 votes |
public void addQueue(final int pPriority, final int pCapacity, final int pInitialCapacity) { if (pCapacity <= 0) { throw new IllegalArgumentException("pCapacity must be greater than 0."); } if (pInitialCapacity <= 0) { throw new IllegalArgumentException("pInitialCapacity must be greater than 0."); } final ReentrantLock lock = this.mLock; lock.lock(); try { this.mQueues.put(pPriority, new CircularList<T>(pInitialCapacity)); this.mQueueCapacities.put(pPriority, pCapacity); this.mNotFullConditions.put(pPriority, this.mLock.newCondition()); } finally { lock.unlock(); } }
Example #14
Source Project: jdk8u60 Author: chenghanpeng File: PriorityBlockingQueue.java License: GNU General Public License v2.0 | 6 votes |
/** * Identity-based version for use in Itr.remove */ void removeEQ(Object o) { final ReentrantLock lock = this.lock; lock.lock(); try { Object[] array = queue; for (int i = 0, n = size; i < n; i++) { if (o == array[i]) { removeAt(i); break; } } } finally { lock.unlock(); } }
Example #15
Source Project: openjdk-jdk8u-backup Author: AdoptOpenJDK File: LinkedBlockingDeque.java License: GNU General Public License v2.0 | 6 votes |
/** * 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 #16
Source Project: canal Author: alibaba File: MemoryEventStoreWithBuffer.java License: Apache License 2.0 | 6 votes |
public void put(List<Event> data) throws InterruptedException, CanalStoreException { if (data == null || data.isEmpty()) { return; } final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { try { while (!checkFreeSlotAt(putSequence.get() + data.size())) { // 检查是否有空位 notFull.await(); // wait until not full } } catch (InterruptedException ie) { notFull.signal(); // propagate to non-interrupted thread throw ie; } doPut(data); if (Thread.interrupted()) { throw new InterruptedException(); } } finally { lock.unlock(); } }
Example #17
Source Project: Bytecoder Author: mirkosertic File: ThreadPoolExecutor.java License: Apache License 2.0 | 6 votes |
/** * Returns the approximate total number of tasks that have ever been * scheduled for execution. Because the states of tasks and * threads may change dynamically during computation, the returned * value is only an approximation. * * @return the number of tasks */ public long getTaskCount() { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { long n = completedTaskCount; for (Worker w : workers) { n += w.completedTasks; if (w.isLocked()) ++n; } return n + workQueue.size(); } finally { mainLock.unlock(); } }
Example #18
Source Project: JDKSourceCode1.8 Author: wupeixuan File: ScheduledThreadPoolExecutor.java License: MIT License | 6 votes |
public void clear() { final ReentrantLock lock = this.lock; lock.lock(); try { for (int i = 0; i < size; i++) { RunnableScheduledFuture<?> t = queue[i]; if (t != null) { queue[i] = null; setIndex(t, -1); } } size = 0; } finally { lock.unlock(); } }
Example #19
Source Project: jdk8u_jdk Author: JetBrains File: LinkedBlockingDeque.java License: GNU General Public License v2.0 | 5 votes |
/** * @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(); } }
Example #20
Source Project: jdk1.8-source-analysis Author: raysonfang File: LinkedBlockingQueue.java License: Apache License 2.0 | 5 votes |
/** * @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(); 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 #21
Source Project: jdk8u60 Author: chenghanpeng File: ThreadPoolExecutor.java License: GNU General Public License v2.0 | 5 votes |
/** * 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 #22
Source Project: openjdk-jdk9 Author: AdoptOpenJDK File: LinkedBlockingDeque.java License: GNU General Public License v2.0 | 5 votes |
public void forEachRemaining(Consumer<? super E> action) { // A variant of forEachFrom Objects.requireNonNull(action); Node<E> p; if ((p = next) == null) return; lastRet = p; next = null; final ReentrantLock lock = LinkedBlockingDeque.this.lock; final int batchSize = 64; Object[] es = null; int n, len = 1; do { lock.lock(); try { if (es == null) { p = nextNode(p); for (Node<E> q = p; q != null; q = succ(q)) if (q.item != null && ++len == batchSize) break; es = new Object[len]; es[0] = nextItem; nextItem = null; n = 1; } else n = 0; for (; p != null && n < len; p = succ(p)) if ((es[n] = p.item) != null) { lastRet = p; n++; } } finally { lock.unlock(); } for (int i = 0; i < n; i++) { @SuppressWarnings("unchecked") E e = (E) es[i]; action.accept(e); } } while (n > 0 && p != null); }
Example #23
Source Project: Rumble Author: Marlinski File: PushService.java License: GNU General Public License v3.0 | 5 votes |
private void signalNotEmpty() { final ReentrantLock takeLock = this.takeLock; takeLock.lock(); try { notEmpty.signal(); } finally { takeLock.unlock(); } }
Example #24
Source Project: Java8CN Author: Java8-CNAPI-Team File: CopyOnWriteArrayList.java License: Apache License 2.0 | 5 votes |
/** * Inserts all of the elements in the specified collection into this * list, starting at the specified position. Shifts the element * currently at that position (if any) and any subsequent elements to * the right (increases their indices). The new elements will appear * in this list in the order that they are returned by the * specified collection's iterator. * * @param index index at which to insert the first element * from the specified collection * @param c collection containing elements to be added to this list * @return {@code true} if this list changed as a result of the call * @throws IndexOutOfBoundsException {@inheritDoc} * @throws NullPointerException if the specified collection is null * @see #add(int,Object) */ public boolean addAll(int index, Collection<? extends E> c) { Object[] cs = c.toArray(); final ReentrantLock lock = this.lock; lock.lock(); try { Object[] elements = getArray(); int len = elements.length; if (index > len || index < 0) throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+len); if (cs.length == 0) return false; int numMoved = len - index; Object[] newElements; if (numMoved == 0) newElements = Arrays.copyOf(elements, len + cs.length); else { newElements = new Object[len + cs.length]; System.arraycopy(elements, 0, newElements, 0, index); System.arraycopy(elements, index, newElements, index + cs.length, numMoved); } System.arraycopy(cs, 0, newElements, index, cs.length); setArray(newElements); return true; } finally { lock.unlock(); } }
Example #25
Source Project: openjdk-jdk9 Author: AdoptOpenJDK File: LinkedBlockingDeque.java License: GNU General Public License v2.0 | 5 votes |
public E peekFirst() { final ReentrantLock lock = this.lock; lock.lock(); try { return (first == null) ? null : first.item; } finally { lock.unlock(); } }
Example #26
Source Project: j2objc Author: google File: LinkedBlockingDeque.java License: Apache License 2.0 | 5 votes |
/** * @throws NullPointerException {@inheritDoc} */ public boolean offerLast(E e) { if (e == null) throw new NullPointerException(); Node<E> node = new Node<E>(e); final ReentrantLock lock = this.lock; lock.lock(); try { return linkLast(node); } finally { lock.unlock(); } }
Example #27
Source Project: hottub Author: dsrg-uoft File: ArrayBlockingQueue.java License: GNU General Public License v2.0 | 5 votes |
public E poll(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { while (count == 0) { if (nanos <= 0) return null; nanos = notEmpty.awaitNanos(nanos); } return dequeue(); } finally { lock.unlock(); } }
Example #28
Source Project: TencentKona-8 Author: Tencent File: CopyOnWriteArrayList.java License: GNU General Public License v2.0 | 5 votes |
public Iterator<E> iterator() { final ReentrantLock lock = l.lock; lock.lock(); try { checkForComodification(); return new COWSubListIterator<E>(l, 0, offset, size); } finally { lock.unlock(); } }
Example #29
Source Project: floodlight_with_topoguard Author: xuraylei File: PriorityPendingQueue.java License: Apache License 2.0 | 5 votes |
private void signalNotFull() { final ReentrantLock putLock = this.putLock; putLock.lock(); try { notFull.signal(); } finally { putLock.unlock(); } }
Example #30
Source Project: android-open-project-demo Author: android-cn File: LinkedBlockingDeque.java License: Apache License 2.0 | 5 votes |
/** * 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(); } }