I want to test if a ThreadLocal
has been initialized without actually initializing it. Of course, the code needs to be thread-safe. Ideally I want something like this:
class TestableThreadLocal<T> extends ThreadLocal<T> {
public boolean isInitialized() {
...
}
}
But how would I implement this method?
Edit: Motivation: I have subclassed a ThreadLocal
to override initialValue()
. However, I do not always need the initialization, in particular because it could cause a memory leak in multi-classloader environments. A simple test would help me write code to avoid the accidental initialization.