Use AtomicBoolean to hold storage status

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1567934 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2014-02-13 14:10:27 +00:00
parent 3ddbbcc251
commit 93ae3c741b
1 changed files with 20 additions and 22 deletions

View File

@ -31,6 +31,7 @@ import java.io.IOException;
import java.lang.ref.ReferenceQueue; import java.lang.ref.ReferenceQueue;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.http.annotation.ThreadSafe; import org.apache.http.annotation.ThreadSafe;
import org.apache.http.client.cache.HttpCacheEntry; import org.apache.http.client.cache.HttpCacheEntry;
@ -60,18 +61,18 @@ public class ManagedHttpCacheStorage implements HttpCacheStorage, Closeable {
private final CacheMap entries; private final CacheMap entries;
private final ReferenceQueue<HttpCacheEntry> morque; private final ReferenceQueue<HttpCacheEntry> morque;
private final Set<ResourceReference> resources; private final Set<ResourceReference> resources;
private final AtomicBoolean active;
private volatile boolean shutdown;
public ManagedHttpCacheStorage(final CacheConfig config) { public ManagedHttpCacheStorage(final CacheConfig config) {
super(); super();
this.entries = new CacheMap(config.getMaxCacheEntries()); this.entries = new CacheMap(config.getMaxCacheEntries());
this.morque = new ReferenceQueue<HttpCacheEntry>(); this.morque = new ReferenceQueue<HttpCacheEntry>();
this.resources = new HashSet<ResourceReference>(); this.resources = new HashSet<ResourceReference>();
this.active = new AtomicBoolean(true);
} }
private void ensureValidState() throws IllegalStateException { private void ensureValidState() throws IllegalStateException {
if (this.shutdown) { if (!this.active.get()) {
throw new IllegalStateException("Cache has been shut down"); throw new IllegalStateException("Cache has been shut down");
} }
} }
@ -134,9 +135,7 @@ public class ManagedHttpCacheStorage implements HttpCacheStorage, Closeable {
} }
public void cleanResources() { public void cleanResources() {
if (this.shutdown) { if (this.active.get()) {
return;
}
ResourceReference ref; ResourceReference ref;
while ((ref = (ResourceReference) this.morque.poll()) != null) { while ((ref = (ResourceReference) this.morque.poll()) != null) {
synchronized (this) { synchronized (this) {
@ -145,12 +144,10 @@ public class ManagedHttpCacheStorage implements HttpCacheStorage, Closeable {
ref.getResource().dispose(); ref.getResource().dispose();
} }
} }
}
public void shutdown() { public void shutdown() {
if (this.shutdown) { if (this.active.compareAndSet(true, false)) {
return;
}
this.shutdown = true;
synchronized (this) { synchronized (this) {
this.entries.clear(); this.entries.clear();
for (final ResourceReference ref: this.resources) { for (final ResourceReference ref: this.resources) {
@ -161,6 +158,7 @@ public class ManagedHttpCacheStorage implements HttpCacheStorage, Closeable {
} }
} }
} }
}
@Override @Override
public void close() { public void close() {