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 long tryOptimisticRead() { return lock.tryOptimisticRead(); }
@Override public long tryOptimisticRead() { return lock.tryOptimisticRead(); }
@SuppressWarnings( "unchecked" ) public StampedCopyOnWriteArrayList(Class<? extends T> clazz) { this.data = (T[]) newInstance( clazz, 0 ); lock = new StampedLock(); optimisticStamp = lock.tryOptimisticRead(); }
public long getCounter() { long stamp = rwlock.tryOptimisticRead(); total++; if (rwlock.validate(stamp)) { success++; return counter; } return counter; }
/** * Return a stamp for read validation. * * @return A stamp, which can be used with {@link #validate(long)}. * @throws InversibleLockException if this lock is currently write-locked */ public long optimisticRead() { while (true) { final long stamp = lock.tryOptimisticRead(); if (stamp != 0) { return stamp; } // Write-locked. Read the corresponding latch and if present report an exception, which will propagate // and force release of locks. final CountDownLatch local = latch; if (local != null) { throw new InversibleLockException(local); } // No latch present: retry optimistic lock } }
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 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 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; }
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; }