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; }
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); StampedLock lock = new StampedLock(); executor.submit(() -> { long stamp = lock.tryOptimisticRead(); try { System.out.println("Optimistic Lock Valid: " + lock.validate(stamp)); ConcurrentUtils.sleep(1); System.out.println("Optimistic Lock Valid: " + lock.validate(stamp)); ConcurrentUtils.sleep(2); System.out.println("Optimistic Lock Valid: " + lock.validate(stamp)); } finally { lock.unlock(stamp); } }); executor.submit(() -> { long stamp = lock.writeLock(); try { System.out.println("Write Lock acquired"); ConcurrentUtils.sleep(2); } finally { lock.unlock(stamp); System.out.println("Write done"); } }); ConcurrentUtils.stop(executor); }
@Override public boolean validate(final long stamp) { return lock.validate(stamp); }
public boolean validate(final long stamp) { return lock.validate(stamp); }
@Override public boolean validate(final long stamp) { return lock.validate(stamp); }
public long getCounter() { long stamp = rwlock.tryOptimisticRead(); total++; if (rwlock.validate(stamp)) { success++; return counter; } return counter; }
@Override public T get(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException { long timeoutTime = System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(timeout, unit); long stamp = lock.tryReadLock(timeout, unit); if (!lock.validate(stamp)) { if (System.currentTimeMillis() > timeoutTime) throw new TimeoutException(); if (isCancelled()) throw new InterruptedException(); } 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 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 T[] getUnderlyingArray() { T[] array = data; if ( lock.validate( optimisticStamp ) ) { return array; } // Acquiring a read lock does not increment the optimistic stamp long stamp = lock.readLock(); try { return data; } finally { lock.unlockRead( 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; }
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; }
/** * Get the number of mutex objects being held * * @return The number of items in the map */ public int getMutexCount() { long stamp = this.lock.tryOptimisticRead(); int size = mutexMap.size(); if (!this.lock.validate(stamp)) { stamp = this.lock.readLock(); try { size = mutexMap.size(); } finally { this.lock.unlockRead(stamp); } } return size; }
private <T> T readValues(Supplier<T> reader) { T result = null; long stamp = stampedLock.tryOptimisticRead(); if (entries.size() > 0) { result = reader.get(); } else { stamp = 0; } if (!stampedLock.validate(stamp)) { stamp = stampedLock.writeLock(); try { ensureMetadataLoaded(); result = reader.get(); } finally { stampedLock.unlockWrite(stamp); } } return result; }
private <T> T readValues(Supplier<T> reader) { T result = null; long stamp = stampedLock.tryOptimisticRead(); if (entries.size() > 0) { result = reader.get(); } else { stamp = 0; } if (!stampedLock.validate(stamp)) { stamp = stampedLock.writeLock(); try { ensureMetadataLoaded(); result = reader.get(); } finally { stampedLock.unlockWrite(stamp); } } return result; }
public HashStreamRanges getRanges() { HashStreamRanges rs; long stamp = lock.tryOptimisticRead(); rs = ranges; if (!lock.validate(stamp)) { stamp = lock.readLock(); try { rs = ranges; } finally { lock.unlockRead(stamp); } } return rs; }
public long getCounter() { long stamp = rwlock.tryOptimisticRead(); try { long result = counter; if (rwlock.validate(stamp)) { return result; } stamp = rwlock.readLock(); result = counter; rwlock.unlockRead(stamp); return counter; } finally { } }
/** * Gets the future associated to the action task. * * @return Future or null if none has been set. */ protected Future<?> getFuture() { long stamp = lock.tryOptimisticRead(); Future<?> future = this.future; if (!lock.validate(stamp)) { // Not valid so wait for read lock stamp = lock.readLock(); try { future = this.future; } finally { lock.unlockRead(stamp); } } return future; }
private StampedLock sl = new StampedLock(); public void edit() { // write method long stamp = sl.writeLock(); try { doEdit(); } finally { sl.unlockWrite(stamp); } } public Object search() { // read method long stamp = sl.tryOptimisticRead(); Object result = doSearch(); //first try without lock, search ideally should be fast if (!sl.validate(stamp)) { //if something has modified stamp = sl.readLock(); //acquire read lock and search again try { result = doSearch(); } finally { sl.unlockRead(stamp); } } return result; }