hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1567936 - /httpcomponents/httpclient/branches/4.3.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.java
Date Thu, 13 Feb 2014 14:13:27 GMT
Author: olegk
Date: Thu Feb 13 14:13:27 2014
New Revision: 1567936

URL: http://svn.apache.org/r1567936
Log:
Use AtomicBoolean to hold storage status

Modified:
    httpcomponents/httpclient/branches/4.3.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.java

Modified: httpcomponents/httpclient/branches/4.3.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.3.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.java?rev=1567936&r1=1567935&r2=1567936&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.3.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.java
(original)
+++ httpcomponents/httpclient/branches/4.3.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.java
Thu Feb 13 14:13:27 2014
@@ -31,6 +31,7 @@ import java.io.IOException;
 import java.lang.ref.ReferenceQueue;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.http.annotation.ThreadSafe;
 import org.apache.http.client.cache.HttpCacheEntry;
@@ -60,18 +61,18 @@ public class ManagedHttpCacheStorage imp
     private final CacheMap entries;
     private final ReferenceQueue<HttpCacheEntry> morque;
     private final Set<ResourceReference> resources;
-
-    private volatile boolean shutdown;
+    private final AtomicBoolean active;
 
     public ManagedHttpCacheStorage(final CacheConfig config) {
         super();
         this.entries = new CacheMap(config.getMaxCacheEntries());
         this.morque = new ReferenceQueue<HttpCacheEntry>();
         this.resources = new HashSet<ResourceReference>();
+        this.active = new AtomicBoolean(true);
     }
 
     private void ensureValidState() throws IllegalStateException {
-        if (this.shutdown) {
+        if (!this.active.get()) {
             throw new IllegalStateException("Cache has been shut down");
         }
     }
@@ -130,30 +131,27 @@ public class ManagedHttpCacheStorage imp
     }
 
     public void cleanResources() {
-        if (this.shutdown) {
-            return;
-        }
-        ResourceReference ref;
-        while ((ref = (ResourceReference) this.morque.poll()) != null) {
-            synchronized (this) {
-                this.resources.remove(ref);
+        if (this.active.get()) {
+            ResourceReference ref;
+            while ((ref = (ResourceReference) this.morque.poll()) != null) {
+                synchronized (this) {
+                    this.resources.remove(ref);
+                }
+                ref.getResource().dispose();
             }
-            ref.getResource().dispose();
         }
     }
 
     public void shutdown() {
-        if (this.shutdown) {
-            return;
-        }
-        this.shutdown = true;
-        synchronized (this) {
-            this.entries.clear();
-            for (final ResourceReference ref: this.resources) {
-                ref.getResource().dispose();
-            }
-            this.resources.clear();
-            while (this.morque.poll() != null) {
+        if (this.active.compareAndSet(true, false)) {
+            synchronized (this) {
+                this.entries.clear();
+                for (final ResourceReference ref: this.resources) {
+                    ref.getResource().dispose();
+                }
+                this.resources.clear();
+                while (this.morque.poll() != null) {
+                }
             }
         }
     }



Mime
View raw message