[concurrency-interest] implementing reentrant locks with Thread.currentThread

Andrew Haley aph at redhat.com
Fri Nov 8 12:08:14 EST 2013


On 10/21/2013 04:21 AM, Andy Nuss wrote:

> I was wondering, if one is building a type of re-entrant lock, do
> you get better performance by paying the cost of
> Thread.currentThread() for each lock operation, or using a
> ThreadLocal variable.  If one chooses the latter, is there an impact
> by having lots of effective thread local storage bloat?

In addition to David Holmes' response, you may be interested to know
just how littel overhead Thread.currentThread() actually is.  On a
modern RISC, it is exactly

       ldr	x13, [xthread,#480]

i.e. the cost of Thread.currentThread() is the same as that of a field
access.  A ThreadLocal variable is considerably more expensive.

Andrew.


More information about the Concurrency-interest mailing list