19 using System.Collections.Generic;
21 using System.Threading;
22 using Lucene.Net.Support;
25 using Lucene.Net.Support.Compatibility;
28 namespace Lucene.Net.Util
56 private ThreadLocal<WeakReference> t =
new ThreadLocal<WeakReference>();
58 private IDictionary<Thread, T> hardRefs =
new HashMap<Thread, T>();
60 private bool isDisposed;
62 public virtual T InitialValue()
67 public virtual T Get()
69 WeakReference weakRef = t.
Get();
72 T iv = InitialValue();
83 return (T)weakRef.Get();
87 public virtual void Set(T @
object)
99 t.Set(
new WeakReference(@
object));
104 hardRefs.Add(Thread.CurrentThread, @
object);
107 var threadsToRemove = hardRefs.Keys.Where(thread => !thread.IsAlive).ToList();
109 foreach (var thread
in threadsToRemove)
111 hardRefs.Remove(thread);
116 [Obsolete(
"Use Dispose() instead")]
117 public virtual void Close()
122 public void Dispose()
127 protected virtual void Dispose(
bool disposing)
129 if (isDisposed)
return;
150 internal static class CloseableThreadLocalExtensions
152 public static void Set<T>(
this ThreadLocal<T> t, T val)
157 public static T Get<T>(
this ThreadLocal<T> t)
162 public static void Remove<T>(
this ThreadLocal<T> t)
167 public static object Get(
this WeakReference w)