Re: Concurrency-interest Digest, Vol 11, Issue 24

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Concurrency-interest Digest, Vol 11, Issue 24

Brice Beard

Sorry if this question has been rehashed a few times, I have been  
reading
the list on and off and can't find a way to search the archive.

We've started using ConcurrentHashMap in earnest on jvm1.5, and I  
want to
confirm my understanding is correct.

In the code below, we want to limit contention on hashtable read  
access to the
minimum but still need synchronization on write.

Looks dangerously like DCL but works because:
- ConcurrentHashMap entry uses a volatile value
- So if the get retrieve a value, that value is guaranteed to be  
completely
constructed
- As a bonus, this would also work on previous jvms as long as the Uri
itself is completely recursuvely built out of volatile data members ?

Is there a standard idiom to do this, is there a way to have a finer
grained synchronization for the put ?

thanks,

brice

ConcurrentHashMap uriMap = ...

Uri getUri(String key)
{
    Uri uri = uriMap.get(key);
    if(uri == null) {
       synchronized (uriCreationMonitor) {
       uri = uriMap.get(key);
       if(uri == null) {
          uri = new UriImpl(...);
          uriMap.put(key, uri);
        }
    }
}

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Re: Concurrency-interest Digest, Vol 11, Issue 24

Joe Bowbeer
On 1/9/06, Brice Beard <[hidden email]> wrote:
>
> In the code below, we want to limit contention on hashtable read
> access to the minimum but still need synchronization on write.
>
> Is there a standard idiom to do this, is there a way to have a finer
> grained synchronization for the put ?
>

I think this may be an application for putIfAbsent -- a special
feature of ConcurrentMap.

See the javadoc

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ConcurrentMap.html

There are also a few slides about this in TS-3423:

http://java.sun.com/javaone/sf/speaker_awards.jsp
 -> http://developers.sun.com/learning/javaoneonline/2005/coreplatform/TS-3423.html

Look for section on Memoize.


>
> ConcurrentHashMap uriMap = ...
>
> Uri getUri(String key)
> {
>     Uri uri = uriMap.get(key);
>     if(uri == null) {
>        synchronized (uriCreationMonitor) {
>        uri = uriMap.get(key);
>        if(uri == null) {
>           uri = new UriImpl(...);
>           uriMap.put(key, uri);
>         }
>     }
> }
>
>

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Re: Concurrency-interest Digest, Vol 11, Issue 24

Brian Goetz
In reply to this post by Brice Beard
> Is there a standard idiom to do this, is there a way to have a finer
> grained synchronization for the put ?

The standard idiom is putIfAbsent.

If putIfAbsent doesn't work for your situation, you'll need to adhere to
a convention like "No one puts anything in the map without the foo
lock", as you suggested.  If that convention is uniformly adhered to,
what you are doing is OK, if a little fragile.

But if you're doing a lot of put'ing, this could be a serious
scalability problem, because now every insertion must wait for a global,
map-wide lock (the CHM implementation uses lock striping to allow writes
to overlap), and performance may revert to something like that of
Hashtable.

> ConcurrentHashMap uriMap = ...
>
> Uri getUri(String key)
> {
>    Uri uri = uriMap.get(key);
>    if(uri == null) {
>       synchronized (uriCreationMonitor) {
>       uri = uriMap.get(key);
>       if(uri == null) {
>          uri = new UriImpl(...);
>          uriMap.put(key, uri);
>        }
>    }
> }

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest