[concurrency-interest] lock acquire/release order in CHM.size()

Tim Eck teck at terracottatech.com
Tue Aug 12 14:52:48 EDT 2008


Just a mild curiosity of mine that folks on this list might have insight
into...

When operations like size() on j.u.c.ConcurrentHashMap have to resort to
locking, the locks are acquired and released like so:

  for (int i = 0; i < segments.length; ++i) 
      segments[i].lock();
  /* ... */ 
  for (int i = 0; i < segments.length; ++i)
      segments[i].unlock();

The curious bit is that the last lock obtained is not the first lock
released. I'm not suggesting there is anything wrong with that, but I was
wondering if there is a specific rationale for that? I suppose two threads
going through those loops would be less serialized since one thread could
start acquiring in lock step with the other thread doing the unlocks. 

thanks
-tim


More information about the Concurrency-interest mailing list