Here are the examples of the java api class java.util.concurrent.locks.ReentrantLock taken from open source projects.
1. ReentrantLockTest#testSerialization()
Project: openjdk
File: ReentrantLockTest.java
File: ReentrantLockTest.java
public void testSerialization(boolean fair) { ReentrantLock lock = new ReentrantLock(fair); lock.lock(); ReentrantLock clone = serialClone(lock); assertEquals(lock.isFair(), clone.isFair()); assertTrue(lock.isLocked()); assertFalse(clone.isLocked()); assertEquals(1, lock.getHoldCount()); assertEquals(0, clone.getHoldCount()); clone.lock(); clone.lock(); assertTrue(clone.isLocked()); assertEquals(2, clone.getHoldCount()); assertEquals(1, lock.getHoldCount()); clone.unlock(); clone.unlock(); assertTrue(lock.isLocked()); assertFalse(clone.isLocked()); }
2. CycleDetectingLockFactoryTest#testDifferentLockFactories_policyExecution()
Project: guava
File: CycleDetectingLockFactoryTest.java
File: CycleDetectingLockFactoryTest.java
public void testDifferentLockFactories_policyExecution() { CycleDetectingLockFactory otherFactory = CycleDetectingLockFactory.newInstance(Policies.WARN); ReentrantLock lockD = otherFactory.newReentrantLock("LockD"); // lockD -> lockA lockD.lock(); lockA.lock(); lockA.unlock(); lockD.unlock(); // lockA -> lockD should warn but otherwise succeed because lockD was // created by a factory with the WARN policy. lockA.lock(); lockD.lock(); }
3. CycleDetectingLockFactoryTest#testDifferentLockFactories()
Project: guava
File: CycleDetectingLockFactoryTest.java
File: CycleDetectingLockFactoryTest.java
public void testDifferentLockFactories() { CycleDetectingLockFactory otherFactory = CycleDetectingLockFactory.newInstance(Policies.WARN); ReentrantLock lockD = otherFactory.newReentrantLock("LockD"); // lockA -> lockD lockA.lock(); lockD.lock(); lockA.unlock(); lockD.unlock(); // lockD -> lockA should fail even though lockD is from a different factory. lockD.lock(); try { lockA.lock(); fail("Expected PotentialDeadlockException"); } catch (PotentialDeadlockException expected) { checkMessage(expected, "LockD -> LockA", "LockA -> LockD"); } }
4. TestDocumentsWriterDeleteQueue#testPartiallyAppliedGlobalSlice()
Project: lucene-solr
File: TestDocumentsWriterDeleteQueue.java
File: TestDocumentsWriterDeleteQueue.java
public void testPartiallyAppliedGlobalSlice() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InterruptedException { final DocumentsWriterDeleteQueue queue = new DocumentsWriterDeleteQueue(); ReentrantLock lock = queue.globalBufferLock; lock.lock(); Thread t = new Thread() { @Override public void run() { queue.addDelete(new Term("foo", "bar")); } }; t.start(); t.join(); lock.unlock(); assertTrue("changes in del queue but not in slice yet", queue.anyChanges()); queue.tryApplyGlobalSlice(); assertTrue("changes in global buffer", queue.anyChanges()); FrozenBufferedUpdates freezeGlobalBuffer = queue.freezeGlobalBuffer(null); assertTrue(freezeGlobalBuffer.any()); assertEquals(1, freezeGlobalBuffer.terms.size()); assertFalse("all changes applied", queue.anyChanges()); }
5. ImagePreprocessor#getApkIconDiskCache()
Project: Sketch
File: ImagePreprocessor.java
File: ImagePreprocessor.java
/** * ??APK????? */ private PreProcessResult getApkIconDiskCache(LoadRequest loadRequest) { String realUri = loadRequest.getAttrs().getRealUri(); Configuration configuration = loadRequest.getSketch().getConfiguration(); File apkFile = new File(realUri); if (!apkFile.exists()) { return null; } long lastModifyTime = apkFile.lastModified(); String diskCacheKey = realUri + "." + lastModifyTime; ReentrantLock diskCacheEditLock = configuration.getDiskCache().getEditLock(diskCacheKey); diskCacheEditLock.lock(); PreProcessResult result = readApkIcon(configuration, loadRequest, diskCacheKey, realUri); diskCacheEditLock.unlock(); return result; }
6. MyImagePreprocessor#getXpkIconCacheFile()
Project: Sketch
File: MyImagePreprocessor.java
File: MyImagePreprocessor.java
/** * ??XPK??????? */ private PreProcessResult getXpkIconCacheFile(LoadRequest loadRequest) { String realUri = loadRequest.getAttrs().getRealUri(); Configuration configuration = loadRequest.getSketch().getConfiguration(); File xpkFile = new File(realUri); if (!xpkFile.exists()) { return null; } long lastModifyTime = xpkFile.lastModified(); String diskCacheKey = realUri + "." + lastModifyTime; ReentrantLock diskCacheEditLock = configuration.getDiskCache().getEditLock(diskCacheKey); diskCacheEditLock.lock(); PreProcessResult result = readXpkIcon(configuration, loadRequest, diskCacheKey, realUri); diskCacheEditLock.unlock(); return result; }
7. MemoryEventStoreWithBuffer#get()
Project: canal
File: MemoryEventStoreWithBuffer.java
File: MemoryEventStoreWithBuffer.java
public Events<Event> get(Position start, int batchSize) throws InterruptedException, CanalStoreException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { try { while (!checkUnGetSlotAt((LogPosition) start, batchSize)) notEmpty.await(); } catch (InterruptedException ie) { notEmpty.signal(); throw ie; } return doGet(start, batchSize); } finally { lock.unlock(); } }
8. MemoryEventStoreWithBuffer#tryPut()
Project: canal
File: MemoryEventStoreWithBuffer.java
File: MemoryEventStoreWithBuffer.java
public boolean tryPut(List<Event> data) throws CanalStoreException { if (data == null || data.isEmpty()) { return true; } final ReentrantLock lock = this.lock; lock.lock(); try { if (!checkFreeSlotAt(putSequence.get() + data.size())) { return false; } else { doPut(data); return true; } } finally { lock.unlock(); } }
9. MemoryEventStoreWithBuffer#put()
Project: canal
File: MemoryEventStoreWithBuffer.java
File: MemoryEventStoreWithBuffer.java
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())) { // ??????? // wait until not full notFull.await(); } } catch (InterruptedException ie) { notFull.signal(); throw ie; } doPut(data); if (Thread.interrupted()) { throw new InterruptedException(); } } finally { lock.unlock(); } }
10. KeyedLock#lock()
Project: astrid
File: KeyedLock.java
File: KeyedLock.java
/** * @param key */ public void lock(K key) { if (DEBUG) { log("acquiring lock for key " + key); } ReentrantLock lock; synchronized (mLocks) { lock = mLocks.get(key); if (lock == null) { lock = new ReentrantLock(); mLocks.put(key, lock); if (DEBUG) { log(lock + " created new lock and added it to map"); } } } lock.lock(); }
11. LinkedBlockingDeque#writeObject()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * Save the state of this deque to a stream (that is, serialize it). * * @param s the stream * @serialData The capacity (int), followed by elements (each an * {@code Object}) in the proper order, followed by a null */ 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(); } }
12. LinkedBlockingDeque#clear()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * 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(); } }
13. LinkedBlockingDeque#toString()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
public String toString() { final ReentrantLock lock = this.lock; lock.lock(); try { Node<E> p = first; if (p == null) return "[]"; StringBuilder sb = new StringBuilder(); sb.append('['); for (; ; ) { E e = p.item; sb.append(e == this ? "(this Collection)" : e); p = p.next; if (p == null) return sb.append(']').toString(); sb.append(',').append(' '); } } finally { lock.unlock(); } }
14. LinkedBlockingDeque#toArray()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: 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/> * <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/> * <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/> * <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}: * <p/> * <pre> * 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(); } }
15. LinkedBlockingDeque#toArray()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/* * TODO: Add support for more efficient bulk operations. * * We don't want to acquire the lock for every iteration, but we * also want other threads a chance to interact with the * collection, especially when count is close to capacity. */ // /** // * Adds all of the elements in the specified collection to this // * queue. Attempts to addAll of a queue to itself result in // * {@code IllegalArgumentException}. Further, the behavior of // * this operation is undefined if the specified collection is // * modified while the operation is in progress. // * // * @param c collection containing elements to be added to this queue // * @return {@code true} if this queue changed as a result of the call // * @throws ClassCastException {@inheritDoc} // * @throws NullPointerException {@inheritDoc} // * @throws IllegalArgumentException {@inheritDoc} // * @throws IllegalStateException {@inheritDoc} // * @see #add(Object) // */ // public boolean addAll(Collection<? extends E> c) { // if (c == null) // throw new NullPointerException(); // if (c == this) // throw new IllegalArgumentException(); // final ReentrantLock lock = this.lock; // lock.lock(); // try { // boolean modified = false; // for (E e : c) // if (linkLast(e)) // modified = true; // return modified; // } finally { // lock.unlock(); // } // } /** * Returns an array containing all of the elements in this deque, in * proper sequence (from first to last element). * <p/> * <p>The returned array will be "safe" in that no references to it are * maintained by this deque. (In other words, this method must allocate * a new array). The caller is thus free to modify the returned array. * <p/> * <p>This method acts as bridge between array-based and collection-based * APIs. * * @return an array containing all of the elements in this deque */ public Object[] toArray() { final ReentrantLock lock = this.lock; lock.lock(); try { Object[] a = new Object[count]; int k = 0; for (Node<E> p = first; p != null; p = p.next) a[k++] = p.item; return a; } finally { lock.unlock(); } }
16. LinkedBlockingDeque#contains()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * 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(); } }
17. LinkedBlockingDeque#drainTo()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * @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(); final ReentrantLock lock = this.lock; lock.lock(); try { int n = Math.min(maxElements, count); for (int i = 0; i < n; i++) { // In this order, in case add() throws. c.add(first.item); unlinkFirst(); } return n; } finally { lock.unlock(); } }
18. LinkedBlockingDeque#removeLastOccurrence()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
public boolean removeLastOccurrence(Object o) { if (o == null) return false; final ReentrantLock lock = this.lock; lock.lock(); try { for (Node<E> p = last; p != null; p = p.prev) { if (o.equals(p.item)) { unlink(p); return true; } } return false; } finally { lock.unlock(); } }
19. LinkedBlockingDeque#removeFirstOccurrence()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
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(); } }
20. LinkedBlockingDeque#pollLast()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
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(); } }
21. LinkedBlockingDeque#pollFirst()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
public E pollFirst(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { E x; while ((x = unlinkFirst()) == null) { if (nanos <= 0) return null; nanos = notEmpty.awaitNanos(nanos); } return x; } finally { lock.unlock(); } }
22. LinkedBlockingDeque#takeLast()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
public E takeLast() throws InterruptedException { final ReentrantLock lock = this.lock; lock.lock(); try { E x; while ((x = unlinkLast()) == null) notEmpty.await(); return x; } finally { lock.unlock(); } }
23. LinkedBlockingDeque#takeFirst()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: 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(); } }
24. LinkedBlockingDeque#offerLast()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ public boolean offerLast(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 (!linkLast(node)) { if (nanos <= 0) return false; nanos = notFull.awaitNanos(nanos); } return true; } finally { lock.unlock(); } }
25. LinkedBlockingDeque#offerFirst()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * @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(); } }
26. LinkedBlockingDeque#putLast()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ public void putLast(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 (!linkLast(node)) notFull.await(); } finally { lock.unlock(); } }
27. LinkedBlockingDeque#putFirst()
Project: Android-Universal-Image-Loader
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * @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(); } }
28. PriorityObjectBlockingQueue#peek()
Project: android-open-project-demo
File: PriorityObjectBlockingQueue.java
File: PriorityObjectBlockingQueue.java
public E peek() { if (count.get() == 0) return null; final ReentrantLock takeLock = this.takeLock; takeLock.lock(); try { Node<E> first = head.next; if (first == null) return null; else return first.getValue(); } finally { takeLock.unlock(); } }
29. PriorityObjectBlockingQueue#poll()
Project: android-open-project-demo
File: PriorityObjectBlockingQueue.java
File: PriorityObjectBlockingQueue.java
public E poll() { final AtomicInteger count = this.count; if (count.get() == 0) return null; E x = null; int c = -1; final ReentrantLock takeLock = this.takeLock; takeLock.lock(); try { if (count.get() > 0) { x = opQueue(null); c = count.getAndDecrement(); if (c > 1) notEmpty.signal(); } } finally { takeLock.unlock(); } if (c == capacity) signalNotFull(); return x; }
30. PriorityObjectBlockingQueue#poll()
Project: android-open-project-demo
File: PriorityObjectBlockingQueue.java
File: PriorityObjectBlockingQueue.java
public E poll(long timeout, TimeUnit unit) throws InterruptedException { E x = null; int c = -1; long nanos = unit.toNanos(timeout); final AtomicInteger count = this.count; final ReentrantLock takeLock = this.takeLock; takeLock.lockInterruptibly(); try { while (count.get() == 0) { if (nanos <= 0) return null; nanos = notEmpty.awaitNanos(nanos); } x = opQueue(null); c = count.getAndDecrement(); if (c > 1) notEmpty.signal(); } finally { takeLock.unlock(); } if (c == capacity) signalNotFull(); return x; }
31. PriorityObjectBlockingQueue#take()
Project: android-open-project-demo
File: PriorityObjectBlockingQueue.java
File: PriorityObjectBlockingQueue.java
public E take() throws InterruptedException { E x; int c = -1; final AtomicInteger count = this.count; final ReentrantLock takeLock = this.takeLock; takeLock.lockInterruptibly(); try { while (count.get() == 0) { notEmpty.await(); } x = opQueue(null); c = count.getAndDecrement(); if (c > 1) notEmpty.signal(); } finally { takeLock.unlock(); } if (c == capacity) signalNotFull(); return x; }
32. PriorityObjectBlockingQueue#offer()
Project: android-open-project-demo
File: PriorityObjectBlockingQueue.java
File: PriorityObjectBlockingQueue.java
public boolean offer(E e) { if (e == null) throw new NullPointerException(); final AtomicInteger count = this.count; if (count.get() == capacity) return false; int c = -1; Node<E> node = new Node<E>(e); final ReentrantLock putLock = this.putLock; putLock.lock(); try { if (count.get() < capacity) { opQueue(node); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } } finally { putLock.unlock(); } if (c == 0) signalNotEmpty(); return c >= 0; }
33. PriorityObjectBlockingQueue#put()
Project: android-open-project-demo
File: PriorityObjectBlockingQueue.java
File: PriorityObjectBlockingQueue.java
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>(e); final ReentrantLock putLock = this.putLock; final AtomicInteger count = this.count; putLock.lockInterruptibly(); try { while (count.get() == capacity) { notFull.await(); } opQueue(node); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } finally { putLock.unlock(); } if (c == 0) signalNotEmpty(); }
34. LinkedBlockingDeque#writeObject()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * 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(); } }
35. LinkedBlockingDeque#clear()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * 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(); } }
36. LinkedBlockingDeque#toString()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
public String toString() { final ReentrantLock lock = this.lock; lock.lock(); try { Node<E> p = first; if (p == null) return "[]"; StringBuilder sb = new StringBuilder(); sb.append('['); for (; ; ) { E e = p.item; sb.append(e == this ? "(this Collection)" : e); p = p.next; if (p == null) return sb.append(']').toString(); sb.append(',').append(' '); } } finally { lock.unlock(); } }
37. LinkedBlockingDeque#toArray()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: 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> * 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(); } }
38. LinkedBlockingDeque#toArray()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/* * TODO: Add support for more efficient bulk operations. * * We don't want to acquire the lock for every iteration, but we * also want other threads a chance to interact with the * collection, especially when count is close to capacity. */ // /** // * Adds all of the elements in the specified collection to this // * queue. Attempts to addAll of a queue to itself result in // * {@code IllegalArgumentException}. Further, the behavior of // * this operation is undefined if the specified collection is // * modified while the operation is in progress. // * // * @param c collection containing elements to be added to this queue // * @return {@code true} if this queue changed as a result of the call // * @throws ClassCastException {@inheritDoc} // * @throws NullPointerException {@inheritDoc} // * @throws IllegalArgumentException {@inheritDoc} // * @throws IllegalStateException {@inheritDoc} // * @see #add(Object) // */ // public boolean addAll(Collection<? extends E> c) { // if (c == null) // throw new NullPointerException(); // if (c == this) // throw new IllegalArgumentException(); // final ReentrantLock lock = this.lock; // lock.lock(); // try { // boolean modified = false; // for (E e : c) // if (linkLast(e)) // modified = true; // return modified; // } finally { // lock.unlock(); // } // } /** * Returns an array containing all of the elements in this deque, in * proper sequence (from first to last element). * * <p>The returned array will be "safe" in that no references to it are * maintained by this deque. (In other words, this method must allocate * a new array). The caller is thus free to modify the returned array. * * <p>This method acts as bridge between array-based and collection-based * APIs. * * @return an array containing all of the elements in this deque */ public Object[] toArray() { final ReentrantLock lock = this.lock; lock.lock(); try { Object[] a = new Object[count]; int k = 0; for (Node<E> p = first; p != null; p = p.next) a[k++] = p.item; return a; } finally { lock.unlock(); } }
39. LinkedBlockingDeque#contains()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * 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(); } }
40. LinkedBlockingDeque#drainTo()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * @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(); final ReentrantLock lock = this.lock; lock.lock(); try { int n = Math.min(maxElements, count); for (int i = 0; i < n; i++) { // In this order, in case add() throws. c.add(first.item); unlinkFirst(); } return n; } finally { lock.unlock(); } }
41. LinkedBlockingDeque#removeLastOccurrence()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
public boolean removeLastOccurrence(Object o) { if (o == null) return false; final ReentrantLock lock = this.lock; lock.lock(); try { for (Node<E> p = last; p != null; p = p.prev) { if (o.equals(p.item)) { unlink(p); return true; } } return false; } finally { lock.unlock(); } }
42. LinkedBlockingDeque#removeFirstOccurrence()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
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(); } }
43. LinkedBlockingDeque#pollLast()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
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(); } }
44. LinkedBlockingDeque#pollFirst()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
public E pollFirst(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { E x; while ((x = unlinkFirst()) == null) { if (nanos <= 0) return null; nanos = notEmpty.awaitNanos(nanos); } return x; } finally { lock.unlock(); } }
45. LinkedBlockingDeque#takeLast()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
public E takeLast() throws InterruptedException { final ReentrantLock lock = this.lock; lock.lock(); try { E x; while ((x = unlinkLast()) == null) notEmpty.await(); return x; } finally { lock.unlock(); } }
46. LinkedBlockingDeque#takeFirst()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: 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(); } }
47. LinkedBlockingDeque#offerLast()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ public boolean offerLast(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 (!linkLast(node)) { if (nanos <= 0) return false; nanos = notFull.awaitNanos(nanos); } return true; } finally { lock.unlock(); } }
48. LinkedBlockingDeque#offerFirst()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * @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(); } }
49. LinkedBlockingDeque#putLast()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ public void putLast(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 (!linkLast(node)) notFull.await(); } finally { lock.unlock(); } }
50. LinkedBlockingDeque#putFirst()
Project: android-open-project-demo
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * @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(); } }
51. ThreadPoolExecutor#shutdownNow()
Project: j2objc
File: ThreadPoolExecutor.java
File: ThreadPoolExecutor.java
/** * Attempts to stop all actively executing tasks, halts the * processing of waiting tasks, and returns a list of the tasks * that were awaiting execution. These tasks are drained (removed) * from the task queue upon return from this method. * * <p>This method does not wait for actively executing tasks to * terminate. Use {@link #awaitTermination awaitTermination} to * do that. * * <p>There are no guarantees beyond best-effort attempts to stop * processing actively executing tasks. This implementation * cancels tasks via {@link Thread#interrupt}, so any task that * fails to respond to interrupts may never terminate. */ // android-note: Removed @throws SecurityException public List<Runnable> shutdownNow() { List<Runnable> tasks; final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { advanceRunState(STOP); interruptWorkers(); tasks = drainQueue(); } finally { mainLock.unlock(); } tryTerminate(); return tasks; }
52. ThreadPoolExecutor#shutdown()
Project: j2objc
File: ThreadPoolExecutor.java
File: ThreadPoolExecutor.java
/** * Initiates an orderly shutdown in which previously submitted * tasks are executed, but no new tasks will be accepted. * Invocation has no additional effect if already shut down. * * <p>This method does not wait for previously submitted tasks to * complete execution. Use {@link #awaitTermination awaitTermination} * to do that. */ // android-note: Removed @throws SecurityException public void shutdown() { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { advanceRunState(SHUTDOWN); interruptIdleWorkers(); // hook for ScheduledThreadPoolExecutor onShutdown(); } finally { mainLock.unlock(); } tryTerminate(); }
53. ThreadPoolExecutor#addWorkerFailed()
Project: j2objc
File: ThreadPoolExecutor.java
File: ThreadPoolExecutor.java
/** * Rolls back the worker thread creation. * - removes worker from workers, if present * - decrements worker count * - rechecks for termination, in case the existence of this * worker was holding up termination */ private void addWorkerFailed(Worker w) { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { if (w != null) workers.remove(w); decrementWorkerCount(); tryTerminate(); } finally { mainLock.unlock(); } }
54. ThreadPoolExecutor#interruptIdleWorkers()
Project: j2objc
File: ThreadPoolExecutor.java
File: ThreadPoolExecutor.java
/** * Interrupts threads that might be waiting for tasks (as * indicated by not being locked) so they can check for * termination or configuration changes. Ignores * SecurityExceptions (in which case some threads may remain * uninterrupted). * * @param onlyOne If true, interrupt at most one worker. This is * called only from tryTerminate when termination is otherwise * enabled but there are still other workers. In this case, at * most one waiting worker is interrupted to propagate shutdown * signals in case all threads are currently waiting. * Interrupting any arbitrary thread ensures that newly arriving * workers since shutdown began will also eventually exit. * To guarantee eventual termination, it suffices to always * interrupt only one idle worker, but shutdown() interrupts all * idle workers so that redundant workers exit promptly, not * waiting for a straggler task to finish. */ private void interruptIdleWorkers(boolean onlyOne) { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { for (Worker w : workers) { Thread t = w.thread; if (!t.isInterrupted() && w.tryLock()) { try { t.interrupt(); } catch (SecurityException ignore) { } finally { w.unlock(); } } if (onlyOne) break; } } finally { mainLock.unlock(); } }
55. PriorityBlockingQueue#toArray()
Project: j2objc
File: PriorityBlockingQueue.java
File: PriorityBlockingQueue.java
/** * Returns an array containing all of the elements in this queue; the * runtime type of the returned array is that of the specified array. * The returned array elements are in no particular order. * If the queue 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 queue. * * <p>If this queue fits in the specified array with room to spare * (i.e., the array has more elements than this queue), the element in * the array immediately following the end of the queue 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 queue known to contain only strings. * The following code can be used to dump the queue 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 queue 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 queue * @throws ArrayStoreException if the runtime type of the specified array * is not a supertype of the runtime type of every element in * this queue * @throws NullPointerException if the specified array is null */ public <T> T[] toArray(T[] a) { final ReentrantLock lock = this.lock; lock.lock(); try { int n = size; if (a.length < n) // Make a new array of a's runtime type, but my contents: return (T[]) Arrays.copyOf(queue, size, a.getClass()); System.arraycopy(queue, 0, a, 0, n); if (a.length > n) a[n] = null; return a; } finally { lock.unlock(); } }
56. PriorityBlockingQueue#clear()
Project: j2objc
File: PriorityBlockingQueue.java
File: PriorityBlockingQueue.java
/** * 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 { Object[] array = queue; int n = size; size = 0; for (int i = 0; i < n; i++) array[i] = null; } finally { lock.unlock(); } }
57. PriorityBlockingQueue#drainTo()
Project: j2objc
File: PriorityBlockingQueue.java
File: PriorityBlockingQueue.java
/** * @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; final ReentrantLock lock = this.lock; lock.lock(); try { int n = Math.min(size, maxElements); for (int i = 0; i < n; i++) { // In this order, in case add() throws. c.add((E) queue[0]); dequeue(); } return n; } finally { lock.unlock(); } }
58. PriorityBlockingQueue#toString()
Project: j2objc
File: PriorityBlockingQueue.java
File: PriorityBlockingQueue.java
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(); } }
59. PriorityBlockingQueue#removeEQ()
Project: j2objc
File: PriorityBlockingQueue.java
File: PriorityBlockingQueue.java
/** * 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(); } }
60. PriorityBlockingQueue#remove()
Project: j2objc
File: PriorityBlockingQueue.java
File: PriorityBlockingQueue.java
/** * 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(); } }
61. PriorityBlockingQueue#poll()
Project: j2objc
File: PriorityBlockingQueue.java
File: PriorityBlockingQueue.java
public E poll(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); E result; try { while ((result = dequeue()) == null && nanos > 0) nanos = notEmpty.awaitNanos(nanos); } finally { lock.unlock(); } return result; }
62. PriorityBlockingQueue#take()
Project: j2objc
File: PriorityBlockingQueue.java
File: PriorityBlockingQueue.java
public E take() throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); E result; try { while ((result = dequeue()) == null) notEmpty.await(); } finally { lock.unlock(); } return result; }
63. PriorityBlockingQueue#offer()
Project: j2objc
File: PriorityBlockingQueue.java
File: PriorityBlockingQueue.java
/** * Inserts the specified element into this priority queue. * As the queue is unbounded, this method will never return {@code false}. * * @param e the element to add * @return {@code true} (as specified by {@link Queue#offer}) * @throws ClassCastException if the specified element cannot be compared * with elements currently in the priority queue according to the * priority queue's ordering * @throws NullPointerException if the specified element is null */ public boolean offer(E e) { if (e == null) throw new NullPointerException(); final ReentrantLock lock = this.lock; lock.lock(); int n, cap; Object[] array; while ((n = size) >= (cap = (array = queue).length)) tryGrow(array, cap); try { Comparator<? super E> cmp = comparator; if (cmp == null) siftUpComparable(n, e, array); else siftUpUsingComparator(n, e, array, cmp); size = n + 1; notEmpty.signal(); } finally { lock.unlock(); } return true; }
64. LinkedBlockingQueue#peek()
Project: j2objc
File: LinkedBlockingQueue.java
File: LinkedBlockingQueue.java
public E peek() { if (count.get() == 0) return null; final ReentrantLock takeLock = this.takeLock; takeLock.lock(); try { Node<E> first = head.next; if (first == null) return null; else return first.item; } finally { takeLock.unlock(); } }
65. LinkedBlockingQueue#poll()
Project: j2objc
File: LinkedBlockingQueue.java
File: LinkedBlockingQueue.java
public E poll() { final AtomicInteger count = this.count; if (count.get() == 0) return null; E x = null; int c = -1; final ReentrantLock takeLock = this.takeLock; takeLock.lock(); try { if (count.get() > 0) { x = dequeue(); c = count.getAndDecrement(); if (c > 1) notEmpty.signal(); } } finally { takeLock.unlock(); } if (c == capacity) signalNotFull(); return x; }
66. LinkedBlockingQueue#poll()
Project: j2objc
File: LinkedBlockingQueue.java
File: LinkedBlockingQueue.java
public E poll(long timeout, TimeUnit unit) throws InterruptedException { E x = null; int c = -1; long nanos = unit.toNanos(timeout); final AtomicInteger count = this.count; final ReentrantLock takeLock = this.takeLock; takeLock.lockInterruptibly(); try { while (count.get() == 0) { if (nanos <= 0) return null; nanos = notEmpty.awaitNanos(nanos); } x = dequeue(); c = count.getAndDecrement(); if (c > 1) notEmpty.signal(); } finally { takeLock.unlock(); } if (c == capacity) signalNotFull(); return x; }
67. LinkedBlockingQueue#take()
Project: j2objc
File: LinkedBlockingQueue.java
File: LinkedBlockingQueue.java
public E take() throws InterruptedException { E x; int c = -1; final AtomicInteger count = this.count; final ReentrantLock takeLock = this.takeLock; takeLock.lockInterruptibly(); try { while (count.get() == 0) { notEmpty.await(); } x = dequeue(); c = count.getAndDecrement(); if (c > 1) notEmpty.signal(); } finally { takeLock.unlock(); } if (c == capacity) signalNotFull(); return x; }
68. LinkedBlockingQueue#offer()
Project: j2objc
File: LinkedBlockingQueue.java
File: LinkedBlockingQueue.java
/** * Inserts the specified element at the tail of this queue if it is * possible to do so immediately without exceeding the queue's capacity, * returning {@code true} upon success and {@code false} if this queue * is full. * When using a capacity-restricted queue, this method is generally * preferable to method {@link BlockingQueue#add add}, which can fail to * insert an element only by throwing an exception. * * @throws NullPointerException if the specified element is null */ public boolean offer(E e) { if (e == null) throw new NullPointerException(); final AtomicInteger count = this.count; if (count.get() == capacity) return false; int c = -1; Node<E> node = new Node<E>(e); final ReentrantLock putLock = this.putLock; putLock.lock(); try { if (count.get() < capacity) { enqueue(node); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } } finally { putLock.unlock(); } if (c == 0) signalNotEmpty(); return c >= 0; }
69. LinkedBlockingDeque#writeObject()
Project: j2objc
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * Saves this deque to a stream (that is, serializes it). * * @serialData The capacity (int), followed by elements (each an * {@code Object}) in the proper order, followed by a null */ 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(); } }
70. LinkedBlockingDeque#clear()
Project: j2objc
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * 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(); } }
71. LinkedBlockingDeque#toString()
Project: j2objc
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
public String toString() { final ReentrantLock lock = this.lock; lock.lock(); try { Node<E> p = first; if (p == null) return "[]"; StringBuilder sb = new StringBuilder(); sb.append('['); for (; ; ) { E e = p.item; sb.append(e == this ? "(this Collection)" : e); p = p.next; if (p == null) return sb.append(']').toString(); sb.append(',').append(' '); } } finally { lock.unlock(); } }
72. LinkedBlockingDeque#toArray()
Project: j2objc
File: LinkedBlockingDeque.java
File: 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(); } }
73. LinkedBlockingDeque#toArray()
Project: j2objc
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/* * TODO: Add support for more efficient bulk operations. * * We don't want to acquire the lock for every iteration, but we * also want other threads a chance to interact with the * collection, especially when count is close to capacity. */ // /** // * Adds all of the elements in the specified collection to this // * queue. Attempts to addAll of a queue to itself result in // * {@code IllegalArgumentException}. Further, the behavior of // * this operation is undefined if the specified collection is // * modified while the operation is in progress. // * // * @param c collection containing elements to be added to this queue // * @return {@code true} if this queue changed as a result of the call // * @throws ClassCastException {@inheritDoc} // * @throws NullPointerException {@inheritDoc} // * @throws IllegalArgumentException {@inheritDoc} // * @throws IllegalStateException {@inheritDoc} // * @see #add(Object) // */ // public boolean addAll(Collection<? extends E> c) { // if (c == null) // throw new NullPointerException(); // if (c == this) // throw new IllegalArgumentException(); // final ReentrantLock lock = this.lock; // lock.lock(); // try { // boolean modified = false; // for (E e : c) // if (linkLast(e)) // modified = true; // return modified; // } finally { // lock.unlock(); // } // } /** * Returns an array containing all of the elements in this deque, in * proper sequence (from first to last element). * * <p>The returned array will be "safe" in that no references to it are * maintained by this deque. (In other words, this method must allocate * a new array). The caller is thus free to modify the returned array. * * <p>This method acts as bridge between array-based and collection-based * APIs. * * @return an array containing all of the elements in this deque */ @SuppressWarnings("unchecked") public Object[] toArray() { final ReentrantLock lock = this.lock; lock.lock(); try { Object[] a = new Object[count]; int k = 0; for (Node<E> p = first; p != null; p = p.next) a[k++] = p.item; return a; } finally { lock.unlock(); } }
74. LinkedBlockingDeque#contains()
Project: j2objc
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * 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(); } }
75. LinkedBlockingDeque#drainTo()
Project: j2objc
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * @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; final ReentrantLock lock = this.lock; lock.lock(); try { int n = Math.min(maxElements, count); for (int i = 0; i < n; i++) { // In this order, in case add() throws. c.add(first.item); unlinkFirst(); } return n; } finally { lock.unlock(); } }
76. LinkedBlockingDeque#removeLastOccurrence()
Project: j2objc
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
public boolean removeLastOccurrence(Object o) { if (o == null) return false; final ReentrantLock lock = this.lock; lock.lock(); try { for (Node<E> p = last; p != null; p = p.prev) { if (o.equals(p.item)) { unlink(p); return true; } } return false; } finally { lock.unlock(); } }
77. LinkedBlockingDeque#removeFirstOccurrence()
Project: j2objc
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
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(); } }
78. LinkedBlockingDeque#pollLast()
Project: j2objc
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
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(); } }
79. LinkedBlockingDeque#pollFirst()
Project: j2objc
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
public E pollFirst(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { E x; while ((x = unlinkFirst()) == null) { if (nanos <= 0) return null; nanos = notEmpty.awaitNanos(nanos); } return x; } finally { lock.unlock(); } }
80. LinkedBlockingDeque#takeLast()
Project: j2objc
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
public E takeLast() throws InterruptedException { final ReentrantLock lock = this.lock; lock.lock(); try { E x; while ((x = unlinkLast()) == null) notEmpty.await(); return x; } finally { lock.unlock(); } }
81. LinkedBlockingDeque#takeFirst()
Project: j2objc
File: LinkedBlockingDeque.java
File: 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(); } }
82. LinkedBlockingDeque#offerLast()
Project: j2objc
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ public boolean offerLast(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 (!linkLast(node)) { if (nanos <= 0) return false; nanos = notFull.awaitNanos(nanos); } return true; } finally { lock.unlock(); } }
83. LinkedBlockingDeque#offerFirst()
Project: j2objc
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * @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(); } }
84. LinkedBlockingDeque#putLast()
Project: j2objc
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ public void putLast(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 (!linkLast(node)) notFull.await(); } finally { lock.unlock(); } }
85. LinkedBlockingDeque#putFirst()
Project: j2objc
File: LinkedBlockingDeque.java
File: LinkedBlockingDeque.java
/** * @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(); } }
86. DelayQueue#removeEQ()
Project: j2objc
File: DelayQueue.java
File: DelayQueue.java
/** * Identity-based version for use in Itr.remove */ void removeEQ(Object o) { final ReentrantLock lock = this.lock; lock.lock(); try { for (Iterator<E> it = q.iterator(); it.hasNext(); ) { if (o == it.next()) { it.remove(); break; } } } finally { lock.unlock(); } }
87. DelayQueue#drainTo()
Project: j2objc
File: DelayQueue.java
File: DelayQueue.java
/** * @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; final ReentrantLock lock = this.lock; lock.lock(); try { int n = 0; for (E e; n < maxElements && (e = peekExpired()) != null; ) { // In this order, in case add() throws. c.add(e); q.poll(); ++n; } return n; } finally { lock.unlock(); } }
88. DelayQueue#drainTo()
Project: j2objc
File: DelayQueue.java
File: DelayQueue.java
/** * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ public int drainTo(Collection<? super E> c) { if (c == null) throw new NullPointerException(); if (c == this) throw new IllegalArgumentException(); final ReentrantLock lock = this.lock; lock.lock(); try { int n = 0; for (E e; (e = peekExpired()) != null; ) { // In this order, in case add() throws. c.add(e); q.poll(); ++n; } return n; } finally { lock.unlock(); } }
89. DelayQueue#poll()
Project: j2objc
File: DelayQueue.java
File: DelayQueue.java
/** * Retrieves and removes the head of this queue, or returns {@code null} * if this queue has no elements with an expired delay. * * @return the head of this queue, or {@code null} if this * queue has no elements with an expired delay */ public E poll() { final ReentrantLock lock = this.lock; lock.lock(); try { E first = q.peek(); if (first == null || first.getDelay(NANOSECONDS) > 0) return null; else return q.poll(); } finally { lock.unlock(); } }
90. DelayQueue#offer()
Project: j2objc
File: DelayQueue.java
File: DelayQueue.java
/** * Inserts the specified element into this delay queue. * * @param e the element to add * @return {@code true} * @throws NullPointerException if the specified element is null */ public boolean offer(E e) { final ReentrantLock lock = this.lock; lock.lock(); try { q.offer(e); if (q.peek() == e) { leader = null; available.signal(); } return true; } finally { lock.unlock(); } }
91. ArrayBlockingQueue#clear()
Project: j2objc
File: ArrayBlockingQueue.java
File: ArrayBlockingQueue.java
/** * Atomically removes all of the elements from this queue. * The queue will be empty after this call returns. */ public void clear() { final Object[] items = this.items; final ReentrantLock lock = this.lock; lock.lock(); try { int k = count; if (k > 0) { 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(); } }
92. ArrayBlockingQueue#toString()
Project: j2objc
File: ArrayBlockingQueue.java
File: ArrayBlockingQueue.java
public String toString() { final ReentrantLock lock = this.lock; lock.lock(); try { int k = count; if (k == 0) return "[]"; final Object[] items = this.items; StringBuilder sb = new StringBuilder(); sb.append('['); for (int i = takeIndex; ; ) { Object e = items[i]; sb.append(e == this ? "(this Collection)" : e); if (--k == 0) return sb.append(']').toString(); sb.append(',').append(' '); if (++i == items.length) i = 0; } } finally { lock.unlock(); } }
93. ArrayBlockingQueue#toArray()
Project: j2objc
File: ArrayBlockingQueue.java
File: ArrayBlockingQueue.java
/** * Returns an array containing all of the elements in this queue, in * proper sequence; the runtime type of the returned array is that of * the specified array. If the queue 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 queue. * * <p>If this queue fits in the specified array with room to spare * (i.e., the array has more elements than this queue), the element in * the array immediately following the end of the queue 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 queue known to contain only strings. * The following code can be used to dump the queue 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 queue 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 queue * @throws ArrayStoreException if the runtime type of the specified array * is not a supertype of the runtime type of every element in * this queue * @throws NullPointerException if the specified array is null */ @SuppressWarnings("unchecked") public <T> T[] toArray(T[] a) { final ReentrantLock lock = this.lock; lock.lock(); try { final Object[] items = this.items; final int count = this.count; final int firstLeg = Math.min(items.length - takeIndex, count); if (a.length < count) { a = (T[]) Arrays.copyOfRange(items, takeIndex, takeIndex + count, a.getClass()); } else { System.arraycopy(items, takeIndex, a, 0, firstLeg); if (a.length > count) a[count] = null; } if (firstLeg < count) System.arraycopy(items, 0, a, firstLeg, putIndex); return a; } finally { lock.unlock(); } }
94. ArrayBlockingQueue#toArray()
Project: j2objc
File: ArrayBlockingQueue.java
File: ArrayBlockingQueue.java
/** * Returns an array containing all of the elements in this queue, in * proper sequence. * * <p>The returned array will be "safe" in that no references to it are * maintained by this queue. (In other words, this method must allocate * a new array). The caller is thus free to modify the returned array. * * <p>This method acts as bridge between array-based and collection-based * APIs. * * @return an array containing all of the elements in this queue */ public Object[] toArray() { final ReentrantLock lock = this.lock; lock.lock(); try { final Object[] items = this.items; final int end = takeIndex + count; final Object[] a = Arrays.copyOfRange(items, takeIndex, end); if (end != putIndex) System.arraycopy(items, 0, a, items.length - takeIndex, putIndex); return a; } finally { lock.unlock(); } }
95. ArrayBlockingQueue#contains()
Project: j2objc
File: ArrayBlockingQueue.java
File: ArrayBlockingQueue.java
/** * 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 ReentrantLock lock = this.lock; lock.lock(); try { if (count > 0) { final Object[] items = this.items; 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(); } }
96. ArrayBlockingQueue#remove()
Project: j2objc
File: ArrayBlockingQueue.java
File: ArrayBlockingQueue.java
/** * 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 this queue contained the specified element * (or equivalently, if this queue changed as a result of the call). * * <p>Removal of interior elements in circular array based queues * is an intrinsically slow and disruptive operation, so should * be undertaken only in exceptional circumstances, ideally * only when the queue is known not to be accessible by other * threads. * * @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) { if (o == null) return false; final ReentrantLock lock = this.lock; lock.lock(); try { if (count > 0) { final Object[] items = this.items; final int putIndex = this.putIndex; int i = takeIndex; do { if (o.equals(items[i])) { removeAt(i); return true; } if (++i == items.length) i = 0; } while (i != putIndex); } return false; } finally { lock.unlock(); } }
97. ArrayBlockingQueue#peek()
Project: j2objc
File: ArrayBlockingQueue.java
File: ArrayBlockingQueue.java
public E peek() { final ReentrantLock lock = this.lock; lock.lock(); try { // The @RetainedLocalRef ensures that the returned item is retained // and autoreleased on the current thread. // null when queue is empty @RetainedLocalRef E result = itemAt(takeIndex); return result; } finally { lock.unlock(); } }
98. ArrayBlockingQueue#poll()
Project: j2objc
File: ArrayBlockingQueue.java
File: ArrayBlockingQueue.java
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(); } }
99. ArrayBlockingQueue#offer()
Project: j2objc
File: ArrayBlockingQueue.java
File: ArrayBlockingQueue.java
/** * Inserts the specified element at the tail of this queue, waiting * up to the specified wait time for space to become available if * the queue is full. * * @throws InterruptedException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { if (e == null) throw new NullPointerException(); long nanos = unit.toNanos(timeout); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { while (count == items.length) { if (nanos <= 0) return false; nanos = notFull.awaitNanos(nanos); } enqueue(e); return true; } finally { lock.unlock(); } }
100. ArrayBlockingQueue#put()
Project: j2objc
File: ArrayBlockingQueue.java
File: ArrayBlockingQueue.java
/** * Inserts the specified element at the tail of this queue, waiting * for space to become available if the queue is full. * * @throws InterruptedException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public void put(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { while (count == items.length) notFull.await(); enqueue(e); } finally { lock.unlock(); } }