long stamp = lock.readLock(); final long newStamp = lock.tryConvertToWriteLock(stamp); lock.unlockRead(stamp); stamp = lock.writeLock(); lock.unlock(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 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 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 ManagedCursor get(String name) { long stamp = rwLock.readLock(); try { Item item = cursors.get(name); return item != null ? item.cursor : null; } finally { rwLock.unlockRead(stamp); } }
private final StampedLock sl = new StampedLock(); long stamp = sl.writeLock(); try { x += deltaX; y += deltaY; } finally { sl.unlockWrite(stamp); long stamp = sl.tryOptimisticRead(); double currentX = x, currentY = y; if (!sl.validate(stamp)) { stamp = sl.readLock(); try { currentX = x; currentY = y; } finally { sl.unlockRead(stamp); long stamp = sl.readLock(); try { while (x == 0.0 && y == 0.0) { long ws = sl.tryConvertToWriteLock(stamp); if (ws != 0L) { stamp = ws; sl.unlockRead(stamp); stamp = sl.writeLock();
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; }
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 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); }
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); }
long ts = lock.tryOptimisticRead(); if (ts != 0) { try { if (lock.validate(ts)) { correctnessLogger.trace("Version, {}", versionForCorrectness); return ret; if (lock.validate(ts)) { throw e; ts = lock.tryConvertToWriteLock(ts); ts = lock.writeLock(); if (lock.validate(ts)) { correctnessLogger.trace("Version, {}", versionForCorrectness); return ret; lock.unlock(ts);
protected ForkedProcessorStep( StageControl control, String name, Configuration config, StatsProvider... statsProviders ) { super( control, name, config, statsProviders ); this.maxProcessors = config.maxNumberOfProcessors(); this.forkedProcessors = new Object[this.maxProcessors]; stripingLock = new StampedLock(); Unit noop = new Unit( -1, null, 0 ); head = new AtomicReference<>( noop ); tail = new AtomicReference<>( noop ); stripingLock.unlock( applyProcessorCount( stripingLock.readLock() ) ); downstreamSender = new CompletedBatchesSender( name + " [CompletedBatchSender]" ); maxQueueLength = 200 + maxProcessors; }
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 ); } }
private long applyProcessorCount( long lock ) { if ( numberOfForkedProcessors != targetNumberOfProcessors ) { stripingLock.unlock( lock ); lock = stripingLock.writeLock(); awaitAllCompleted(); int processors = targetNumberOfProcessors; while ( numberOfForkedProcessors < processors ) { if ( forkedProcessors[numberOfForkedProcessors] == null ) { forkedProcessors[numberOfForkedProcessors] = new ForkedProcessor( numberOfForkedProcessors, tail.get() ); } numberOfForkedProcessors++; } if ( numberOfForkedProcessors > processors ) { numberOfForkedProcessors = processors; // Excess processors will notice that they are not needed right now, and will park until they are. // The most important thing here is that future Units will have a lower number of processor as expected max. } } return lock; }
@Override public void execute(Runnable executeTask) { long stamp = this.lock.tryReadLock(); if (stamp != 0L) { try { executeTask.run(); } finally { this.lock.unlock(stamp); } } }
FileRefSource(ChannelMaster master) { super(master); this.references = PlatformDependent.newMpscQueue(); this.semaphore = new StampedLock(); // throw away a writeLock so that we can immediately park the consumer this.semaphore.writeLock(); this.shortCircuited = false; }
@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 ConcurrentLWJGLBufferAllocator() { this.stampedLock = new StampedLock(); }
@Override public void close(Runnable closeTask) { // Allow only one thread to close if (this.closed.compareAndSet(false, true)) { // Closing is final - we don't need the stamp this.lock.writeLock(); closeTask.run(); } } }