Proposed new Java feature
- From: "Mike Schilling" <mscottschilling@xxxxxxxxxxx>
- Date: Sat, 26 May 2012 16:11:13 -0700
First a few observations:
1. ThreadLocals may be, in general, an abomination, but there are situation
where they're the least of evils. And if you're building a framework in
which third-party code runs (e.g. a webserver), there's no way to disallow
2. ThreadLocals interact badly with ThreadPools, because the ThreadLocals
keep their value when the tyhread is put back into the pool. This can lead
to leaks and even potential security issues.
3. The current implementation of ThreadLocal is this: each thread contains a
map from the ThreadLocal instance to its value for that thread. (This is
slightly less intiutive than giving a ThreadLocal instance a map from Thread
to value, but has the advantage that the map, which is only used within one
thread, needn't be synchronized.)
Proposed feature: a static method on Thread that clears all ThreadLocals for
the current thread. By 3 it's trivial to implement. By 1 and 2 it's
needed. And ThreadPool implementations can simply call it directly before
putting the Thread back into the pool.
- Prev by Date: Re: multithreaded cache?
- Next by Date: Re: Patricia trie vs binary search.
- Previous by thread: c&c++
- Next by thread: Re: Proposed new Java feature