In 2009, some JVMs implemented ThreadLocal
using an unsynchronised HashMap
in the Thread.currentThread()
object. This made it extremely fast (though not nearly as fast as using a regular field access, of course), as well as ensuring that the ThreadLocal
object got tidied up when the Thread
died. Updating this answer in 2016, it seems most (all?) newer JVMs use a ThreadLocalMap
with linear probing. I am uncertain about the performance of those – but I cannot imagine it is significantly worse than the earlier implementation.
Of course, new Object()
is also very fast these days, and the garbage collectors are also very good at reclaiming short-lived objects.
Unless you are certain that object creation is going to be expensive, or you need to persist some state on a thread by thread basis, you are better off going for the simpler allocate when needed solution, and only switching over to a ThreadLocal
implementation when a profiler tells you that you need to.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…