diff --git a/spring-core/src/main/java/org/springframework/util/ConcurrentLruCache.java b/spring-core/src/main/java/org/springframework/util/ConcurrentLruCache.java index ee11b0ca8f7..279e75b0ebf 100644 --- a/spring-core/src/main/java/org/springframework/util/ConcurrentLruCache.java +++ b/spring-core/src/main/java/org/springframework/util/ConcurrentLruCache.java @@ -102,7 +102,7 @@ public final class ConcurrentLruCache { if (this.capacity == 0) { return this.generator.apply(key); } - final Node node = this.cache.get(key); + Node node = this.cache.get(key); if (node == null) { V value = this.generator.apply(key); put(key, value); @@ -115,9 +115,9 @@ public final class ConcurrentLruCache { private void put(K key, V value) { Assert.notNull(key, "key must not be null"); Assert.notNull(value, "value must not be null"); - final CacheEntry cacheEntry = new CacheEntry<>(value, CacheEntryState.ACTIVE); - final Node node = new Node<>(key, cacheEntry); - final Node prior = this.cache.putIfAbsent(node.key, node); + CacheEntry cacheEntry = new CacheEntry<>(value, CacheEntryState.ACTIVE); + Node node = new Node<>(key, cacheEntry); + Node prior = this.cache.putIfAbsent(node.key, node); if (prior == null) { processWrite(new AddTask(node)); } @@ -128,7 +128,7 @@ public final class ConcurrentLruCache { private void processRead(Node node) { boolean drainRequested = this.readOperations.recordRead(node); - final DrainStatus status = this.drainStatus.get(); + DrainStatus status = this.drainStatus.get(); if (status.shouldDrainBuffers(drainRequested)) { drainOperations(); } @@ -228,7 +228,7 @@ public final class ConcurrentLruCache { * {@code false} if there was no matching key */ public boolean remove(K key) { - final Node node = this.cache.remove(key); + Node node = this.cache.remove(key); if (node == null) { return false; } @@ -237,27 +237,29 @@ public final class ConcurrentLruCache { return true; } - /* + /** * Transition the node from the {@code active} state to the {@code pending removal} state, * if the transition is valid. */ private void markForRemoval(Node node) { - for (; ; ) { - final CacheEntry current = node.get(); + while (true) { + CacheEntry current = node.get(); if (!current.isActive()) { return; } - final CacheEntry pendingRemoval = new CacheEntry<>(current.value, CacheEntryState.PENDING_REMOVAL); + CacheEntry pendingRemoval = new CacheEntry<>(current.value, CacheEntryState.PENDING_REMOVAL); if (node.compareAndSet(current, pendingRemoval)) { return; } } } + /** * Write operation recorded when a new entry is added to the cache. */ private final class AddTask implements Runnable { + final Node node; AddTask(Node node) { @@ -275,7 +277,7 @@ public final class ConcurrentLruCache { private void evictEntries() { while (currentSize.get() > capacity) { - final Node node = evictionQueue.poll(); + Node node = evictionQueue.poll(); if (node == null) { return; } @@ -283,7 +285,6 @@ public final class ConcurrentLruCache { markAsRemoved(node); } } - } @@ -291,6 +292,7 @@ public final class ConcurrentLruCache { * Write operation recorded when an entry is removed to the cache. */ private final class RemovalTask implements Runnable { + final Node node; RemovalTask(Node node) { @@ -310,7 +312,7 @@ public final class ConcurrentLruCache { */ private enum DrainStatus { - /* + /** * No drain operation currently running. */ IDLE { @@ -320,7 +322,7 @@ public final class ConcurrentLruCache { } }, - /* + /** * A drain operation is required due to a pending write modification. */ REQUIRED { @@ -330,7 +332,7 @@ public final class ConcurrentLruCache { } }, - /* + /** * A drain operation is in progress. */ PROCESSING { @@ -367,12 +369,6 @@ public final class ConcurrentLruCache { private static final int BUFFER_COUNT = detectNumberOfBuffers(); - private static int detectNumberOfBuffers() { - int availableProcessors = Runtime.getRuntime().availableProcessors(); - int nextPowerOfTwo = 1 << (Integer.SIZE - Integer.numberOfLeadingZeros(availableProcessors - 1)); - return Math.min(4, nextPowerOfTwo); - } - private static final int BUFFERS_MASK = BUFFER_COUNT - 1; private static final int MAX_PENDING_OPERATIONS = 32; @@ -383,19 +379,13 @@ public final class ConcurrentLruCache { private static final int BUFFER_INDEX_MASK = BUFFER_SIZE - 1; - /* - * Number of operations recorded, for each buffer - */ + // Number of operations recorded, for each buffer private final AtomicLongArray recordedCount = new AtomicLongArray(BUFFER_COUNT); - /* - * Number of operations read, for each buffer - */ + // Number of operations read, for each buffer private final long[] readCount = new long[BUFFER_COUNT]; - /* - * Number of operations processed, for each buffer - */ + // Number of operations processed, for each buffer private final AtomicLongArray processedCount = new AtomicLongArray(BUFFER_COUNT); @SuppressWarnings("rawtypes") @@ -403,10 +393,11 @@ public final class ConcurrentLruCache { private final EvictionQueue evictionQueue; + @SuppressWarnings("rawtypes") ReadOperations(EvictionQueue evictionQueue) { this.evictionQueue = evictionQueue; for (int i = 0; i < BUFFER_COUNT; i++) { - this.buffers[i] = new AtomicReferenceArray<>(BUFFER_SIZE); + this.buffers[i] = new AtomicReferenceArray(BUFFER_SIZE); } } @@ -458,6 +449,12 @@ public final class ConcurrentLruCache { } this.processedCount.lazySet(bufferIndex, writeCount); } + + private static int detectNumberOfBuffers() { + int availableProcessors = Runtime.getRuntime().availableProcessors(); + int nextPowerOfTwo = 1 << (Integer.SIZE - Integer.numberOfLeadingZeros(availableProcessors - 1)); + return Math.min(4, nextPowerOfTwo); + } } @@ -536,10 +533,9 @@ public final class ConcurrentLruCache { if (this.first == null) { return null; } - final Node f = this.first; - final Node next = f.getNext(); + Node f = this.first; + Node next = f.getNext(); f.setNext(null); - this.first = next; if (next == null) { this.last = null; @@ -558,13 +554,12 @@ public final class ConcurrentLruCache { } private boolean contains(Node e) { - return (e.getPrevious() != null) || (e.getNext() != null) || (e == this.first); + return (e.getPrevious() != null || e.getNext() != null || e == this.first); } - private void linkLast(final Node e) { - final Node l = this.last; + private void linkLast(Node e) { + Node l = this.last; this.last = e; - if (l == null) { this.first = e; } @@ -575,8 +570,8 @@ public final class ConcurrentLruCache { } private void unlink(Node e) { - final Node prev = e.getPrevious(); - final Node next = e.getNext(); + Node prev = e.getPrevious(); + Node next = e.getNext(); if (prev == null) { this.first = next; }