public ManagedCursor get(String name) { long stamp = rwLock.readLock(); try { Item item = cursors.get(name); return item != null ? item.cursor : null; } finally { rwLock.unlockRead(stamp); } }
/** * Get the slowest reader position, meaning older acknowledged position between all the cursors. * * @return the slowest reader position */ public PositionImpl getSlowestReaderPosition() { long stamp = rwLock.readLock(); try { return heap.isEmpty() ? null : heap.get(0).position; } finally { rwLock.unlockRead(stamp); } }
public ManagedCursor getSlowestReader() { long stamp = rwLock.readLock(); try { return heap.isEmpty() ? null : heap.get(0).cursor; } finally { rwLock.unlockRead(stamp); } }
private <T> T readConcurrentlyWithoutSpin(final Supplier<T> readSupplier) { T result; long stamp = stampedLock.readLock(); try { result = readSupplier.get(); } finally { stampedLock.unlockRead(stamp); } return result; }
@Override public String toString() { long stamp = rwLock.readLock(); try { StringBuilder sb = new StringBuilder(); sb.append('['); boolean first = true; for (Item item : cursors.values()) { if (!first) { sb.append(", "); } first = false; sb.append(item.cursor); } sb.append(']'); return sb.toString(); } finally { rwLock.unlockRead(stamp); } }
private static long convertToWriteLock(long stamp) { final long writeStamp = lock.tryConvertToWriteLock(stamp); if (writeStamp == 0L) { lock.unlockRead(stamp); stamp = lock.writeLock(); } else { stamp = writeStamp; } return stamp; }
public boolean isEmpty() { long stamp = rwLock.tryOptimisticRead(); boolean isEmpty = heap.isEmpty(); if (!rwLock.validate(stamp)) { // Fallback to read lock stamp = rwLock.readLock(); try { isEmpty = heap.isEmpty(); } finally { rwLock.unlockRead(stamp); } } return isEmpty; }
private <T> T readConcurrently(final Supplier<T> readSupplier) { T result; long stamp; for (int i = 0; i < RETRIES; i++) { stamp = stampedLock.tryOptimisticRead(); if(stamp == 0) { continue; } result = readSupplier.get(); if (stampedLock.validate(stamp)) { return result; } } stamp = stampedLock.readLock(); try { result = readSupplier.get(); } finally { stampedLock.unlockRead(stamp); } return result; }
/** * Returns {@code true} if the specified {@code remoteAddress} is known to have no support for * the specified {@link SessionProtocol}. */ public static boolean isUnsupported(SocketAddress remoteAddress, SessionProtocol protocol) { final String key = key(remoteAddress); final CacheEntry e; final long stamp = lock.readLock(); try { e = cache.get(key); } finally { lock.unlockRead(stamp); } if (e == null) { // Can't tell if it's unsupported return false; } return e.isUnsupported(protocol); }
lock.unlockRead(stamp); stamp = lock.writeLock();
public int getQueueSize() { long stamp = stampedLock.tryOptimisticRead(); int queueSize = requestQueue.size(); if (!stampedLock.validate(stamp)) { stamp = stampedLock.readLock(); try { queueSize = requestQueue.size(); } finally { stampedLock.unlockRead(stamp); } } return queueSize; }
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); }
/** * @return the maven configuration to use for the runtime. */ public static MavenConfiguration getMavenConfig() { long stamp = lock.readLock(); try { if (mavenConfig == null) { long writeStamp = lock.tryConvertToWriteLock(stamp); if (writeStamp == 0L) { lock.unlockRead(stamp); stamp = lock.writeLock(); } else { stamp = writeStamp; } if (mavenConfig == null) { initialiseGlobalConfig(); } } return mavenConfig; } finally { lock.unlock(stamp); } }
long writeStamp = lock.tryConvertToWriteLock(stamp); if (writeStamp == 0L) { lock.unlockRead(stamp); stamp = lock.writeLock(); } else {
@Override public void unlockRead(final long stamp) { lock.unlockRead(stamp); }
@Override public T get() { long stamp = lock.readLock(); try { return backing.get(); } catch (Exception e) { Discord4J.LOGGER.error(LogMarkers.UTIL, "Exception caught attempting to handle a ratelimited request", e); } finally { lock.unlockRead(stamp); } return null; }
public static boolean hasId(int id) { long lock_stamp = lock.readLock(); try { id = Math.abs(id); return (id < idToString.size()); } finally{ lock.unlockRead(lock_stamp); } }
public static int size() { long lock_stamp = lock.readLock(); try { return idToString.size(); } finally { lock.unlockRead(lock_stamp); } }
@Override public void releaseReadLock(LockHandle handle) { LockInstance instance = (LockInstance) handle.handle; instance.lock.unlockRead(handle.stamp); returnLockForKey(instance, handle.key); }
private void archiveGorillaStoreCurrentCompressors() { long stamp = gorillaMapLock.readLock(); try { for (Map.Entry<String, Map<TaggedMetric, GorillaStore>> entry : gorillaMap.entrySet()) { for (GorillaStore store : entry.getValue().values()) { store.archiveCurrentCompressor(); } } } finally { gorillaMapLock.unlockRead(stamp); } }