How much is read from
Running unpublished benchmarks,
ThreadLocal.get takes around 35 cycle per iteration on my machine. Not a great deal. In Sun's implementation a custom linear probing hash map in
ThreadLocals to values. Because it is only ever accessed by a single thread, it can be very fast.
Allocation of small objects take a similar number of cycles, although because of cache exhaustion you may get somewhat lower figures in a tight loop.
MessageDigest is likely to be relatively expensive. It has a fair amount of state and construction goes through the
Provider SPI mechanism. You may be able to optimise by, for instance, cloning or providing the
Just because it may be faster to cache in a
ThreadLocal rather than create does not necessarily mean that the system performance will increase. You will have additional overheads related to GC which slows everything down.
Unless your application very heavily uses
MessageDigest you might want to consider using a conventional thread-safe cache instead.