/** * {@inheritDoc} */ @Override public void clear() { final long stamp = lock.writeLock(); try { cacheMap.clear(); } finally { lock.unlockWrite(stamp); } }
private <T> T writeConcurrently(final Supplier<T> writeSupplier) { T result; long stamp = stampedLock.writeLock(); try { result = writeSupplier.get(); } finally { stampedLock.unlockWrite(stamp); } return result; } }
/** * {@inheritDoc} */ @Override public Map<K, V> snapshot() { final long stamp = lock.writeLock(); try { Map<K, V> map = new HashMap<>(cacheMap.size()); cacheMap.forEach((key, cacheValue) -> map.put(key, cacheValue.getObject())); return map; } finally { lock.unlockWrite(stamp); } }
public void removeCursor(String name) { long stamp = rwLock.writeLock(); try { Item item = cursors.remove(name); // Move the item to the right end of the heap to be removed Item lastItem = heap.get(heap.size() - 1); swap(item, lastItem); heap.remove(item.idx); // Update the heap siftDown(lastItem); } finally { rwLock.unlockWrite(stamp); } }
/** * {@inheritDoc} */ @Override public final int prune() { final long stamp = lock.writeLock(); try { return pruneCache(); } finally { lock.unlockWrite(stamp); } }
@Override protected void freeMemory() { final long stamp = stampedLock.writeLock(); try { super.freeMemory(); } finally { stampedLock.unlockWrite(stamp); } } }
@Override public void destroyDirectBuffer(final Buffer buffer) { final long stamp = stampedLock.writeLock(); try { super.destroyDirectBuffer(buffer); } finally { stampedLock.unlockWrite(stamp); } }
@Override public ByteBuffer allocate(final int size) { final long stamp = stampedLock.writeLock(); try { return super.allocate(size); } finally { stampedLock.unlockWrite(stamp); } }
public void add(ManagedCursor cursor) { long stamp = rwLock.writeLock(); try { // Append a new entry at the end of the list Item item = new Item(cursor, heap.size()); cursors.put(cursor.getName(), item); heap.add(item); siftUp(item); } finally { rwLock.unlockWrite(stamp); } }
/** * {@inheritDoc} */ @Override public V remove(final K key) { V removedValue = null; final long stamp = lock.writeLock(); try { CacheObject<K,V> co = cacheMap.remove(key); if (co != null) { onRemove(co.key, co.cachedObject); removedValue = co.cachedObject; } } finally { lock.unlockWrite(stamp); } return removedValue; }
/** * {@inheritDoc} */ @Override public void put(final K key, final V object, final long timeout) { Objects.requireNonNull(object); final long stamp = lock.writeLock(); try { CacheObject<K,V> co = new CacheObject<>(key, object, timeout); if (timeout != 0) { existCustomTimeout = true; } if (isReallyFull(key)) { pruneCache(); } cacheMap.put(key, co); } finally { lock.unlockWrite(stamp); } }
/** * Signal that a cursor position has been updated and that the container must re-order the cursor list. * * @param cursor * @return a pair of positions, representing the previous slowest consumer and the new slowest consumer (after the * update). */ public Pair<PositionImpl, PositionImpl> cursorUpdated(ManagedCursor cursor, Position newPosition) { checkNotNull(cursor); long stamp = rwLock.writeLock(); try { Item item = cursors.get(cursor.getName()); if (item == null) { return null; } PositionImpl previousSlowestConsumer = heap.get(0).position; // When the cursor moves forward, we need to push it toward the // bottom of the tree and push it up if a reset was done item.position = (PositionImpl) newPosition; if (item.idx == 0 || getParent(item).position.compareTo(item.position) <= 0) { siftDown(item); } else { siftUp(item); } PositionImpl newSlowestConsumer = heap.get(0).position; return Pair.of(previousSlowestConsumer, newSlowestConsumer); } finally { rwLock.unlockWrite(stamp); } }
/** * Resets the maven configuration. If new system properties were added, those will be taken into account after reloading the * config. */ public static void reset() { long stamp = lock.writeLock(); try { mavenConfig = null; invalidateCaches(); initialiseGlobalConfig(); } finally { lock.unlockWrite(stamp); } }
public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(2); Map<String, String> map = new HashMap<>(); StampedLock lock = new StampedLock(); executor.submit(() -> { long stamp = lock.writeLock(); try { ConcurrentUtils.sleep(1); map.put("foo", "bar"); } finally { lock.unlockWrite(stamp); } }); Runnable readTask = () -> { long stamp = lock.readLock(); try { System.out.println(map.get("foo")); ConcurrentUtils.sleep(1); } finally { lock.unlockRead(stamp); } }; executor.submit(readTask); executor.submit(readTask); ConcurrentUtils.stop(executor); }
private void writeValues(Runnable writer) { long stamp = stampedLock.writeLock(); try { writer.run(); } finally { stampedLock.unlockWrite(stamp); } } }
private <T> T writeConcurrently(final Supplier<T> writeSupplier) { T result; long stamp = stampedLock.writeLock(); try { result = writeSupplier.get(); } finally { stampedLock.unlockWrite(stamp); } return result; } }
public void unlockWrite(final long stamp) { final CountDownLatch local = LATCH_UPDATER.getAndSet(this, null); Verify.verifyNotNull(local); lock.unlockWrite(stamp); local.countDown(); }
@Override public ByteBuffer allocate(final int size) { final long stamp = stampedLock.writeLock(); try { return super.allocate(size); } finally { stampedLock.unlockWrite(stamp); } }
public void add(ManagedCursor cursor) { long stamp = rwLock.writeLock(); try { // Append a new entry at the end of the list Item item = new Item(cursor, heap.size()); cursors.put(cursor.getName(), item); heap.add(item); siftUp(item); } finally { rwLock.unlockWrite(stamp); } }
public void add(ManagedCursor cursor) { long stamp = rwLock.writeLock(); try { // Append a new entry at the end of the list Item item = new Item(cursor, heap.size()); cursors.put(cursor.getName(), item); heap.add(item); siftUp(item); } finally { rwLock.unlockWrite(stamp); } }