According to ThreadLocal‘s javadoc, it sounds like its a thread-specific container for 1+ atomic fields.
Is the purpose of ThreadLocal to represent all the atomic fields for a single Thread, or is it to just provided a convenience container when you have multiple Atomic* instances that need to be logically grouped together?
I guess I’m wondering why I would ever want to use a ThreadLocal instead of, say, an AtomicLong or AtomicInteger? Thanks in advance!
The purpose of
ThreadLocalis that the fields do not have to be atomic — there is no need to synchronize their values with centralized memory. They are thread-local and only exist in the threads local memory storage.ThreadLocalis very useful for storing a per-thread copy of something. For example aSimpleDateFormatwhich is unfortunately not reentrant.This is a useful pattern because then we don’t have to
synchronizeon it or worry about anyvolatileor other atomic operations that have memory barriers.One caveat is that
ThreadLocalvariables often are detected as memory leaks since they are only freed when the thread is reaped which can cause problems with web container class loaders.