[concurrency-interest] Minor generics nit in AtomicReferenceArray

David M. Lloyd david.lloyd at redhat.com
Thu Mar 26 18:27:05 EDT 2009


He may have a valid point there.  It was, in fact, the collections 
framework that got me into the mess in the first place.  On the other hand, 
having a strong generic parameter in this case has not yet helped me catch 
a single bug (that I'm aware of), but it has caused a problem several times 
for me today alone.  Not that I've had tons of occasions to use 
AtomicReferenceArray, to be fair.

- DML

On 03/26/2009 05:01 PM, Kevin Bourrillion wrote:
> Interesting.  I'll just note that the standard set by the Collections 
> Framework agrees with you:  only parameters which might cause harm are 
> restricted to E, never the innocuous ones.  Some, for example Bill Pugh, 
> disagree with this, saying that because it's so rare to need to query 
> with a non-E, the benefits of supporting that scenario are fewer than 
> those of catching more bugs in the usual use cases.
> 
> 
> 
> On Thu, Mar 26, 2009 at 2:26 PM, David M. Lloyd <david.lloyd at redhat.com 
> <mailto:david.lloyd at redhat.com>> wrote:
> 
>     It would be nice if the "expect" parameter of the two compareAndSet
>     methods in AtomicReferenceArray would be declared as "Object" rather
>     than "E". It's sometimes the case that the expect value is of an
>     unknown class, and this fact doesn't affect operation of the method
>     in any way, yet one still must annoyingly cast the parameter.
> 
>     Here's a patch which illustrates the change.  As far as I'm aware,
>     this won't affect compatibilty in any way.
> 
>     - DML
> 
>     diff -r dde3fe2e8164
>     src/share/classes/java/util/concurrent/atomic/AtomicReferenceArray.java
>     ---
>     a/src/share/classes/java/util/concurrent/atomic/AtomicReferenceArray.java
>     Wed Feb 25 14:32:01 2009 +0000
>     +++
>     b/src/share/classes/java/util/concurrent/atomic/AtomicReferenceArray.java
>     Thu Mar 26 16:18:34 2009 -0500
>     @@ -159,7 +159,7 @@
>          * @return true if successful. False return indicates that
>          * the actual value was not equal to the expected value.
>          */
>     -    public final boolean compareAndSet(int i, E expect, E update) {
>     +    public final boolean compareAndSet(int i, Object expect, E
>     update) {
>             return unsafe.compareAndSwapObject(array, rawIndex(i),
>                                              expect, update);
>         }
>     @@ -177,7 +177,7 @@
>          * @param update the new value
>          * @return true if successful.
>          */
>     -    public final boolean weakCompareAndSet(int i, E expect, E update) {
>     +    public final boolean weakCompareAndSet(int i, Object expect, E
>     update) {
>             return compareAndSet(i, expect, update);
>         }
> 
> 
>     _______________________________________________
>     Concurrency-interest mailing list
>     Concurrency-interest at cs.oswego.edu
>     <mailto:Concurrency-interest at cs.oswego.edu>
>     http://cs.oswego.edu/mailman/listinfo/concurrency-interest
> 
> 
> 
> 
> -- 
> Kevin Bourrillion @ Google
> internal:  http://go/javalibraries
> google-collections.googlecode.com <http://google-collections.googlecode.com>
> google-guice.googlecode.com <http://google-guice.googlecode.com>
> 


More information about the Concurrency-interest mailing list