9. ThreadLocal type
ThreadLocal
allows you to store a variable that would be unique on each thread, and hence, writing
or reading on that variable need not be synchronized.
It comes with several dangers:
- Methods across classes dependent on global states in
ThreadLocal
. - In case a thread is reused, such as that of a thread pool, the initial state of the thread might not be clean, causing unexpected behaviours for the code.