private long elapsedNanos() { return isRunning ? ticker.read() - startTick + elapsedNanos : elapsedNanos; }
/** * Starts the stopwatch. * * @return this {@code Stopwatch} instance * @throws IllegalStateException if the stopwatch is already running. */ @Nonnull public Stopwatch start() { checkState(!isRunning, "This stopwatch is already running."); isRunning = true; startTick = ticker.read(); return this; }
/** * Stops the stopwatch. Future reads will return the fixed duration that had * elapsed up to this point. * * @return this {@code Stopwatch} instance * @throws IllegalStateException if the stopwatch is already stopped. */ @Nonnull public Stopwatch stop() { long tick = ticker.read(); checkState(isRunning, "This stopwatch is already stopped."); isRunning = false; elapsedNanos += tick - startTick; return this; }
void cleanUp() { long now = map.ticker.read(); runLockedCleanup(now); runUnlockedCleanup(); }
@Nullable V waitForLoadingValue(@Nonnull ReferenceEntry<K, V> e, K key, @Nonnull ValueReference<K, V> valueReference) throws ExecutionException { if (!valueReference.isLoading()) { throw new AssertionError(); } Preconditions.checkState(!Thread.holdsLock(e), "Recursive load of: %s", key); // don't consider expiration as we're concurrent with loading try { V value = valueReference.waitForValue(); if (value == null) { throw new InvalidCacheLoadException("CacheLoader returned null for key " + key + "."); } // re-read ticker now that loading has completed long now = map.ticker.read(); recordRead(e, now); return value; } finally { } }
abstract class AbstractCacheSet<T> extends AbstractSet<T> { final ConcurrentMap<?, ?> map; AbstractCacheSet(ConcurrentMap<?, ?> map) { this.map = map; } @Override public int size() { return map.size(); } @Override public boolean isEmpty() { return map.isEmpty(); } @Override public void clear() { map.clear(); } // super.toArray() may misbehave if size() is inaccurate, at least on old versions of Android. // https://code.google.com/p/android/issues/detail?id=36519 / http://r.android.com/47508 @Nonnull @Override public Object[] toArray() { return toArrayList(this).toArray(); } @Nonnull @Override public <E> E[] toArray(E[] a) { return toArrayList(this).toArray(a); } }
long now = ticker.read(); final Segment<K, V>[] segments = this.segments; long last = -1L;
boolean containsKey(Object key, int hash) { try { if (count != 0) { // read-volatile long now = map.ticker.read(); ReferenceEntry<K, V> e = getLiveEntry(key, hash, now); if (e == null) { return false; } return e.getValueReference().get() != null; } return false; } finally { postReadCleanup(); } }
@Nullable V get(Object key, int hash) { try { if (count != 0) { // read-volatile long now = map.ticker.read(); ReferenceEntry<K, V> e = getLiveEntry(key, hash, now); if (e == null) { return null; } V value = e.getValueReference().get(); if (value != null) { recordRead(e, now); return scheduleRefresh(e, e.getKey(), hash, value, now, map.defaultLoader); } tryDrainReferenceQueues(); } return null; } finally { postReadCleanup(); } }
long now = map.ticker.read(); V value = getLiveValue(e, now); if (value != null) {
try { long now = map.ticker.read(); preWriteCleanup(now);
lock(); try { long now = map.ticker.read(); preWriteCleanup(now);
lock(); try { long now = map.ticker.read(); preWriteCleanup(now);
lock(); try { long now = map.ticker.read(); preWriteCleanup(now);
boolean remove(Object key, int hash, Object value) { lock(); try { long now = map.ticker.read(); preWriteCleanup(now);
lock(); try { long now = map.ticker.read(); preWriteCleanup(now);
lock(); try { long now = map.ticker.read(); preWriteCleanup(now);
boolean replace(K key, int hash, V oldValue, V newValue) { lock(); try { long now = map.ticker.read(); preWriteCleanup(now);