lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r831248 - /lucene/java/trunk/src/java/org/apache/lucene/util/CloseableThreadLocal.java
Date Fri, 30 Oct 2009 10:06:55 GMT
Author: mikemccand
Date: Fri Oct 30 10:06:54 2009
New Revision: 831248

URL: http://svn.apache.org/viewvc?rev=831248&view=rev
Log:
LUCENE-2017: improve javadocs of CloseableThreadLocal; forcefully remove calling threads entry
on close

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/util/CloseableThreadLocal.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/util/CloseableThreadLocal.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/util/CloseableThreadLocal.java?rev=831248&r1=831247&r2=831248&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/util/CloseableThreadLocal.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/util/CloseableThreadLocal.java Fri Oct 30
10:06:54 2009
@@ -17,11 +17,11 @@
  * limitations under the License.
  */
 
-import java.util.Map;
+import java.io.Closeable;
+import java.lang.ref.WeakReference;
 import java.util.HashMap;
 import java.util.Iterator;
-import java.lang.ref.WeakReference;
-import java.io.Closeable;
+import java.util.Map;
 
 /** Java's builtin ThreadLocal has a serious flaw:
  *  it can take an arbitrarily long amount of time to
@@ -41,7 +41,14 @@
  *  separately holding a hard reference to each stored
  *  value.  When you call {@link #close}, these hard
  *  references are cleared and then GC is freely able to
- *  reclaim space by objects stored in it. */
+ *  reclaim space by objects stored in it.
+ *
+ *  We can not rely on {@link ThreadLocal#remove()} as it
+ *  only removes the value for the caller thread, whereas
+ *  {@link #close} takes care of all
+ *  threads.  You should not call {@link #close} until all
+ *  threads are done using the instance.
+ */
 
 public class CloseableThreadLocal<T> implements Closeable {
 
@@ -88,6 +95,11 @@
     // all values we were storing are weak (unless somewhere
     // else is still using them) and so GC may reclaim them:
     hardRefs = null;
+    // Take care of the current thread right now; others will be
+    // taken care of via the WeakReferences.
+    if (t != null) {
+      t.remove();
+    }
     t = null;
   }
 }



Mime
View raw message