[concurrency-interest] ConcurrentHashMap in JDK7 code explanation (scanAndLockForPut)
dl at cs.oswego.edu
Mon Aug 11 06:34:42 EDT 2014
> The source codes of the method scanAndLockForPut in ConcurrentHashMap in
> JDK7 says: ...
> I understand what the codes mean, but what I don't is this else if entry:
> else if ((retries & 1) == 0 && (f = entryForHash(this, hash)) != first)
> My question is: Why do we have to do "(retries & 1) == 0"?
> EDIT: I kind of figure it out. It's all because the constant
> static final int MAX_SCAN_RETRIES =
> Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1;
> In single core processor, MAX_SCAN_RETRIES = 1. So the second time the
> thread steps into the loop "while(tryLock)", it doesn't have to check
> whether the first node was changed.
> However, in multi cores processor, this will behave like checking whether
> the first node is changed every 2 times in the while loop.
> Is the above explanation
Yes. We need only ensure that staleness is eventually detected.
Alternating the head-checks works fine, and simplifies use of
the same code for both uni- and multi- processors.
More information about the Concurrency-interest