[concurrency-interest] Question about Double-checked locking

David Holmes dcholmes at optusnet.com.au
Sun May 14 05:41:18 EDT 2006


I forgot to mention that the HashMap isn't protected by a lock at all during
the get() operations, and get()'s can't happen concurrently with put()'s
(though multiple get()'s can be concurrent).

David Holmes

> -----Original Message-----
> From: concurrency-interest-bounces at cs.oswego.edu
> [mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of David
> Holmes
> Sent: Sunday, 14 May 2006 7:30 PM
> To: Tutika Chakravarthy; concurrency-interest at cs.oswego.edu
> Subject: RE: [concurrency-interest] Question about Double-checked
> locking
>
>
> Yes it is the failed double-checked-locking idiom. But it is also
> broken in
> the locking strategy anyway - see below.
>
> No changing from synchronized to Lock doesn't make a difference to the
> double-check part. You must establish a "happens-before" relationship
> between the thread setting the value in the map, and the thread
> reading it.
>
> The locking strategy is broken because you use a different Object for
> different keys. So multiple threads can be concurrently trying to put()
> different key/result pairs. The hashtable must be protected from
> concurrent
> access using the *same* Lock/object in all threads.
>
> Cheers,
> David Holmes
>
>
> > -----Original Message-----
> > From: concurrency-interest-bounces at cs.oswego.edu
> > [mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Tutika
> > Chakravarthy
> > Sent: Sunday, 14 May 2006 7:17 PM
> > To: concurrency-interest at cs.oswego.edu
> > Subject: [concurrency-interest] Question about Double-checked locking
> >
> >
> > Hi All,
> > I have some code like this :
> >
> > result = map.get(key);
> >
> >
> > if (result == null ) {
> >
> > Object mutex = mutexFactory.getMutex(key);
> >
> > synchronized (mutex) {
> > result = map.get(key);
> >
> > if (result == null) {
> > 	result = dataSource.load(key);
> >
> > 	if (result != null)
> > 	map.put(key, result);
> > 	else
> > 	mutexFactory.removeMutex(key);
> >
> > 	}
> >  }
> > }
> >
> > Here mutexFactory returns different monitors based on
> > the key .
> > I am using HashMap to store and retrieve the values .
> > If the value is not there in the map , I should load
> > the value from DataBase using dataSource.
> >
> > I would like to know whether the above code falls
> > under "Double-checked locking" problem.
> >
> >
> > If so , Can I avoid it using new util.concurrent.Lock
> > interface classes ?
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Tired of spam?  Yahoo! Mail has the best spam protection around
> > http://mail.yahoo.com
> > _______________________________________________
> > Concurrency-interest mailing list
> > Concurrency-interest at altair.cs.oswego.edu
> > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest



More information about the Concurrency-interest mailing list