ThreadLocal values can leak
Thread pools reuse threads, so ThreadLocal values survive beyond one request unless removed. Call remove in a finally block when the value is request-scoped.
Good to see "ThreadLocal values can leak" written down. Handy reference for anyone working with java / threads.
Important
Impressive
- @sohmeh▲ 1
"ThreadLocal values can leak" is a useful thing to know about with java / threads. Thanks for sharing it.
Smart
Because thread pools hand the same thread to new work, ThreadLocal state carries over unless it is cleared. A finally block that calls remove keeps request-scoped data from leaking into later tasks.
Pooled threads are reused, so a ThreadLocal value set for one request remains visible to the next task on that thread. Remove it in a finally block when the value belongs to a single request.
InheritableThreadLocalvalues are copied at thread creation, which can leak request state into pool threads created from a request thread. Corrections or other cases welcome below.Specific and to the point: "ThreadLocal values can leak". Worth a look if you touch java / threads.
Good to see "ThreadLocal values can leak" written down. Handy reference for anyone working with java / threads.
Specific and to the point: "ThreadLocal values can leak". Worth a look if you touch java / threads.
Clear note on java / threads. "ThreadLocal values can leak" is the kind of detail that is easy to overlook.