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:
parent
3ddbbcc251
commit
93ae3c741b
|
@ -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,30 +135,27 @@ public class ManagedHttpCacheStorage implements HttpCacheStorage, Closeable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cleanResources() {
|
public void cleanResources() {
|
||||||
if (this.shutdown) {
|
if (this.active.get()) {
|
||||||
return;
|
ResourceReference ref;
|
||||||
}
|
while ((ref = (ResourceReference) this.morque.poll()) != null) {
|
||||||
ResourceReference ref;
|
synchronized (this) {
|
||||||
while ((ref = (ResourceReference) this.morque.poll()) != null) {
|
this.resources.remove(ref);
|
||||||
synchronized (this) {
|
}
|
||||||
this.resources.remove(ref);
|
ref.getResource().dispose();
|
||||||
}
|
}
|
||||||
ref.getResource().dispose();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
if (this.shutdown) {
|
if (this.active.compareAndSet(true, false)) {
|
||||||
return;
|
synchronized (this) {
|
||||||
}
|
this.entries.clear();
|
||||||
this.shutdown = true;
|
for (final ResourceReference ref: this.resources) {
|
||||||
synchronized (this) {
|
ref.getResource().dispose();
|
||||||
this.entries.clear();
|
}
|
||||||
for (final ResourceReference ref: this.resources) {
|
this.resources.clear();
|
||||||
ref.getResource().dispose();
|
while (this.morque.poll() != null) {
|
||||||
}
|
}
|
||||||
this.resources.clear();
|
|
||||||
while (this.morque.poll() != null) {
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue