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; }
final long newStamp = lock.tryConvertToWriteLock(stamp);
/** * enqueue the attempt into our underlying queue. since it's expensive to dynamically * resize the queue, we have a separate rejection threshold which, if less than 0 is * ignored, but otherwise is the practical cap on the size of the queue. */ private boolean tryEnqueueAttempt(DelayedExecution<T> delayedExecution) { int rejectionThreshold = queueRejectionThreshold.get(); if (rejectionThreshold < 0) { return requestQueue.offer(delayedExecution); } long stamp = stampedLock.readLock(); try { while (requestQueue.size() < rejectionThreshold) { long writeStamp = stampedLock.tryConvertToWriteLock(stamp); if (writeStamp != 0L) { stamp = writeStamp; return requestQueue.offer(delayedExecution); } else { stampedLock.unlock(stamp); stamp = stampedLock.writeLock(); } } return false; } finally { stampedLock.unlock(stamp); } }
public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(2); StampedLock lock = new StampedLock(); executor.submit(() -> { long stamp = lock.readLock(); try { if (count == 0) { stamp = lock.tryConvertToWriteLock(stamp); if (stamp == 0L) { System.out.println("Could not convert to write lock"); stamp = lock.writeLock(); } count = 23; } System.out.println(count); } finally { lock.unlock(stamp); } }); 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); } }
MavenConfiguration updatedMavenConfiguration = getMavenConfig(); if (!mavenRuntimeConfig.equals(updatedMavenConfiguration)) { long writeStamp = lock.tryConvertToWriteLock(stamp); if (writeStamp == 0L) { lock.unlockRead(stamp);
public Map<TaggedMetric, GorillaStore> getGorillaStores(String metric) { Map<TaggedMetric, GorillaStore> returnedMap = new HashMap<>(); Map<TaggedMetric, GorillaStore> metricMap; long stamp = gorillaMapLock.readLock(); try { metricMap = gorillaMap.get(metric); if (metricMap == null) { metricMap = new HashMap<>(); long writeStamp = gorillaMapLock.tryConvertToWriteLock(stamp); if (writeStamp == 0) { gorillaMapLock.unlockRead(stamp); stamp = gorillaMapLock.writeLock(); } else { stamp = writeStamp; } gorillaMap.put(metric, metricMap); } returnedMap.putAll(metricMap); } finally { gorillaMapLock.unlock(stamp); } return returnedMap; }
@Override public boolean remove(Object element) { long stamp = lock.readLock(); try { boolean found = true; while ( found ) { T[] array = data; found = false; for ( int index = array.length - 1; index >= 0; index-- ) { if ( element == array[index] ) { found = true; long writeStamp = lock.tryConvertToWriteLock( stamp ); if ( writeStamp != 0 ) { stamp = writeStamp; T[] newData = copyOf( data, data.length - 1 ); arraycopy( data, index + 1, newData, index, data.length - index - 1 ); data = newData; return true; } else { break; } } } } return false; } finally { optimisticStamp = lock.tryConvertToOptimisticRead( stamp ); } }
/** * enqueue the attempt into our underlying queue. since it's expensive to dynamically * resize the queue, we have a separate rejection threshold which, if less than 0 is * ignored, but otherwise is the practical cap on the size of the queue. */ private boolean tryEnqueueAttempt(DelayedExecution<T> delayedExecution) { int rejectionThreshold = queueRejectionThreshold.get(); if (rejectionThreshold < 0) { return requestQueue.offer(delayedExecution); } long stamp = stampedLock.readLock(); try { while (requestQueue.size() < rejectionThreshold) { long writeStamp = stampedLock.tryConvertToWriteLock(stamp); if (writeStamp != 0L) { stamp = writeStamp; return requestQueue.offer(delayedExecution); } else { stampedLock.unlock(stamp); stamp = stampedLock.writeLock(); } } return false; } finally { stampedLock.unlock(stamp); } }
public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(2); StampedLock lock = new StampedLock(); executor.submit(() -> { long stamp = lock.readLock(); try { if (count == 0) { stamp = lock.tryConvertToWriteLock(stamp); if (stamp == 0L) { System.out.println("Could not convert to write lock"); stamp = lock.writeLock(); } count = 23; } System.out.println(count); } finally { lock.unlock(stamp); } }); ConcurrentUtils.stop(executor); }
long writeStamp = gorillaMapLock.tryConvertToWriteLock(stamp); if (writeStamp == 0) { gorillaMapLock.unlockRead(stamp);
long ws = this.lock.tryConvertToWriteLock(stamp); if (ws == 0L) { this.lock.unlockRead(stamp);
ts = lock.tryConvertToWriteLock(ts);
final long newStamp = lock.tryConvertToWriteLock(stamp);
/** * @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); } }
MavenConfiguration updatedMavenConfiguration = getMavenConfig(); if (!mavenRuntimeConfig.equals(updatedMavenConfiguration)) { long writeStamp = lock.tryConvertToWriteLock(stamp); if (writeStamp == 0L) { lock.unlockRead(stamp);
try { while (x == 0.0 && y == 0.0) { long ws = sl.tryConvertToWriteLock(stamp); if (ws != 0L) { stamp = ws;