[concurrency-interest] Question about Double-checked locking

David Holmes dcholmes at optusnet.com.au
Sun May 14 17:35:50 EDT 2006


> Can you elaborate "happens-before" relationship part?

The happens-before relationship is the formal part of the Java Memory Model
that controls the possible outcomes for concurrent reads and writes of a
variable. If a thread A writes a value V to a variable X and thread B reads
X, then for B to see V the write by A must "happen-before" the read by B.
Otherwise B can see a value previously written to X.

Within a thread each program statement happens-before the next, according to
program order. To create cross-thread happens-before relationships you have
to use synchronization tools, like Locks, sync regions, or volatiles. In
simple terms if thread B acquires a given lock after thread A releases it,
then everything that happened in thread A before the releases happens-before
anything in thread B that occurs after the acquire - so any values written
by A will be read by B.

I'm sure someone else can give you a quick link to further details.

> Suppose If I replace HashMap wih ConcurrentHashMap,
> How can I avoid double-checked locking problem.
> My Aim is that if the value is not present in the map
> for a key , load it from Database. Otherwise simply
> use get() method  and return it.

There's a good answer coming on this - stay tuned.

Right now I have to fly.

Cheers,
David Holmes



More information about the Concurrency-interest mailing list