Skip to content
Advertisement

Tag: thread-local

Accessing Java ThreadLocal object from a class other than where it was declared

I’m declaring a ThreadLocal object and setting a value like below. Is there anyway to reference this numberThreaLocalObj variable outside of this class within the same thread? I’ve found some code the seems to clear ALL the threadlocals, but I just need to clear this particular Threadlocal variable based on a condition. Unfortunately this is a inherited technical design. EDIT

How to clean up ThreadLocals

Does any one have an example how to do this? Are they handled by the garbage collector? I’m using Tomcat 6. Answer The javadoc says this: “Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; after a thread goes away, all of its

Performance of ThreadLocal variable

How much is read from ThreadLocal variable slower than from regular field? More concretely is simple object creation faster or slower than access to ThreadLocal variable? I assume that it is fast enough so that having ThreadLocal<MessageDigest> instance is much faster then creating instance of MessageDigest every time. But does that also apply for byte[10] or byte[1000] for example? Edit:

Advertisement