org.apache.lucene.util

## Class CloseableThreadLocal

- java.lang.Object
- - org.apache.lucene.util.CloseableThreadLocal

-     public class CloseableThreadLocal
      extends java.lang.Object

  Java's builtin ThreadLocal has a serious flaw: it can take an arbitrarily long amount of time to dereference the things you had stored in it, even once the ThreadLocal instance itself is no longer referenced. This is because there is single, master map stored for each thread, which all ThreadLocals share, and that master map only periodically purges "stale" entries. While not technically a memory leak, because eventually the memory will be reclaimed, it can take a long time and you can easily hit OutOfMemoryError because from the GC's standpoint the stale entries are not reclaimable. This class works around that, by only enrolling WeakReference values into the ThreadLocal, and separately holding a hard reference to each stored value. When you call `close()`, these hard references are cleared and then GC is freely able to reclaim space by objects stored in it.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `CloseableThreadLocal()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()`
    `java.lang.Object` | `get()`
    `protected java.lang.Object` | `initialValue()`
    `void` | `set(java.lang.Object object)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### CloseableThreadLocal

          public CloseableThreadLocal()

  - ### Method Detail

    - #### initialValue

          protected java.lang.Object initialValue()

    - #### get

          public java.lang.Object get()

    - #### set

          public void set(java.lang.Object object)

    - #### close

          public void close()

Copyright © 2000-2016 Apache Software Foundation. All Rights Reserved.