Answer by Joshua Engel:
represents the flow of control, i.e. the code that is running as a thread. It can be started, interrupted, waited for, assigned a priority, etc.ThreadLocal
represents a piece of memory attached to the thread. It's a single object that maintains a different value for each thread that uses it. When a thread callsset
, it willget
back the same value later, even if other threads have also calledset
on the sameThreadLocal
in the intervening time. Each will get back its own value.
You could achieve the same result with aMap<Thread, ?>
indexed with the current thread, but this is neater and already checked to be thread-safe.