In a library, I have some ThreadLocal
variable with some objects. I want to accumulate all these objects in one data structure and be able to iterate over them when needed. One the other hand, I don't want this data structure get polluted (additionally, this could potentially lead to memory leaks) when application scenario includes creation and death of thousands of threads. So the solution should clean data structure from objects, local to dead threads.
I think solution should be similar to WeakHashMap
with WeakReference<Thread>
in keys, which should additionally check if the Thread
is not alive in the regular expunge procedure. Iteration is perfromed over Map.values()
.
Are there are existing implementations of such data structure? Or there are some better solutions for my problem?