[concurrency-interest] Which lock to use

Doug Lea dl@cs.oswego.edu
Wed, 25 Feb 2004 07:23:21 -0500

> I have an application that is based on cached data that is being updated
> periodically (once every 5 hours).
> I have a scheduler that is responsible for invalidating data so that data
> refresh can be made.
> My problem is which locking system to use for the time of data refresh
> operation (I still have data readers coming in but need to hold them until I
> refresh the data).
> Are Objects like ReadWriteLock suitable for this kind of case? Should I stay
> with regular synch? What options do I have here?
> Any help would be appreciated.

This doesn't say enough to make a concrete recommendation but it is
the sort of situation where read/write locks should work well.  Also,
if (1) your cache is structured as a map (2) it is OK for readers to
use previous values even while updates are being performed (rather
than blocking until updates complete), then you should be able to use
a ConcurrentHashMap as the cache, without any custom coding.