I am looking for a class similar to ThreadLocal which would work on thread groups instead of threads.
If there is not such a class (in some open source library) how
ThreadGroup
is rarely used, so there isn't platform support.
Using [Weak
/Identity
/Concurrent
]HashMap
will just about work, if not very fast. You really want the map to be all weak, identity and concurrent but with the Java library you only get to choose one, currently.
To improve performance, note that Thread
s do not change ThreadGroup
. Therefore, cache the value with a ThreadLocal
(override initialValue
). ThreadLocal
has good performance (a couple dozen cycles per get
).