Gotcha in ConcurrentHashMap; need better docs?

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

Gotcha in ConcurrentHashMap; need better docs?

Elias Ross-5

Take a look at this:

Map m = new ConcurrentHashMap();
m.put("foo", "bar");
Iterator i = m.entrySet().iterator();
Map.Entry me = (Map.Entry) i.next();
m.remove("foo"); // in another thread
me.getValue(); // returns null

The JavaDoc:

> Like Hashtable but unlike HashMap, this class does NOT allow null to
> be used as a key or value.

I'm wondering if there was some way to enhance the documentation so that
people wouldn't assume that Map.Entry.getValue() never returns null.

JBoss bug:

http://jira.jboss.com/jira/browse/JBAS-2973


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

Re: Gotcha in ConcurrentHashMap; need better docs?

Doug Lea
Elias Ross wrote:

> Take a look at this:
>
> Map m = new ConcurrentHashMap();
> m.put("foo", "bar");
> Iterator i = m.entrySet().iterator();
> Map.Entry me = (Map.Entry) i.next();
> m.remove("foo"); // in another thread
> me.getValue(); // returns null
>
> The JavaDoc:
>
>> Like Hashtable but unlike HashMap, this class does NOT allow null to
>> be used as a key or value.
>
> I'm wondering if there was some way to enhance the documentation so that
> people wouldn't assume that Map.Entry.getValue() never returns null.
>

Better yet, the Mustang version does not return null; instead it
returns a snapshot of accessed value. See
   http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6312056

Doing otherwise was arguably a bug (which is why we filed a bug
against ourselves.)  While almost no one
has a good intuition about what "should" happen, no one
expects null to be returned.

(Sorry that this like lots of other small things wait until
Mustang for full release, even though the codebase was changed
last summer.)

-Doug

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

Re: Gotcha in ConcurrentHashMap; need better docs?

Elias Ross-5
On Fri, 2006-04-14 at 13:44 -0400, Doug Lea wrote:

> Better yet, the Mustang version does not return null; instead it
> returns a snapshot of accessed value. See
>    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6312056
>
> Doing otherwise was arguably a bug (which is why we filed a bug
> against ourselves.)  While almost no one
> has a good intuition about what "should" happen, no one
> expects null to be returned.
>

Returning the snapshot value is probably the best solution.

I didn't see the snapshot approach listed in the bug or bug comments.  I
also haven't seen anything mentioned in the JavaDoc here:

http://download.java.net/jdk6/docs/api/java/util/concurrent/ConcurrentHashMap.html#entrySet()


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

Re: Gotcha in ConcurrentHashMap; need better docs?

Doug Lea
Elias Ross wrote:
>
>
> Returning the snapshot value is probably the best solution.
>
>  I
> also haven't seen anything mentioned in the JavaDoc here:
>

This is because the docs said and still way that the iterator
is "weakly consistent" which it now is, even in this corner-case
sense.

-Doug



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