ThreadLocal with null initial values - avoid create map and entry?

Tom Hawtin tom.hawtin at oracle.com
Tue Nov 18 11:37:34 UTC 2014


On 18/11/2014 05:11, Bernd Eckenfels wrote:

> Unfortunatelly the ThreadLocal#get() will call #setInitialValue() in
> all cases, and this will in turn call createMap(). setInitialValue()
> could avoid to call createMap() when value is null. This would reduce
> the number of created thread specific maps and entries (and also weak
> references to the thread).

There's no guarantee that the execution of initialValue was trivial even 
if it returns null.

I don't know whether or not mixing up the execution paths through get 
would harm performance.

Tom



More information about the core-libs-dev mailing list