From 501d8c59e4538c4b60616bead9bf6059d5d57e31 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Sun, 18 Oct 2009 21:44:01 +0000 Subject: [PATCH] LUCENE-1257: Generify CloseableThreadLocal git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@826532 13f79535-47bb-0310-9956-ffa450edef68 --- .../lucene/util/CloseableThreadLocal.java | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/java/org/apache/lucene/util/CloseableThreadLocal.java b/src/java/org/apache/lucene/util/CloseableThreadLocal.java index 4aa72ab1455..4c998f6581b 100644 --- a/src/java/org/apache/lucene/util/CloseableThreadLocal.java +++ b/src/java/org/apache/lucene/util/CloseableThreadLocal.java @@ -42,20 +42,20 @@ import java.lang.ref.WeakReference; * references are cleared and then GC is freely able to * reclaim space by objects stored in it. */ -public class CloseableThreadLocal { +public class CloseableThreadLocal { - private ThreadLocal t = new ThreadLocal(); + private ThreadLocal> t = new ThreadLocal>(); - private Map hardRefs = new HashMap(); + private Map hardRefs = new HashMap(); - protected Object initialValue() { + protected T initialValue() { return null; } - public Object get() { - WeakReference weakRef = (WeakReference) t.get(); + public T get() { + WeakReference weakRef = t.get(); if (weakRef == null) { - Object iv = initialValue(); + T iv = initialValue(); if (iv != null) { set(iv); return iv; @@ -66,17 +66,16 @@ public class CloseableThreadLocal { } } - public void set(Object object) { + public void set(T object) { - t.set(new WeakReference(object)); + t.set(new WeakReference(object)); synchronized(hardRefs) { hardRefs.put(Thread.currentThread(), object); // Purge dead threads - Iterator it = hardRefs.keySet().iterator(); - while(it.hasNext()) { - Thread t = (Thread) it.next(); + for (Iterator it = hardRefs.keySet().iterator(); it.hasNext();) { + final Thread t = it.next(); if (!t.isAlive()) it.remove(); }