[concurrency-interest] Array element compareAndSet

Doug Lea dl at cs.oswego.edu
Tue Dec 16 15:18:26 EST 2008


Doug Lea wrote:
> Roman Elizarov wrote:
>>  I would rather create
>> a separate "AtomicArrays" class where compareAndSet, set, and get
>> methods are declared for Object[], int[], and long[]. There will be
>> analogy with "java.util.Arrays". It will be easy to learn and use just
>> like "Arrays".
> 
> This might indeed be a better compromise. It would take some hard work
> to make the reference version efficient -- it would need to
> emulate ArrayStoreException checks at Java level, but in
> a way that JVMs could subject to the usual machinations that
> usually optimize them away. I'll look into it.
> 

It seems that a better way to do this is to add three
methods to java.lang.reflect.Array:
   compareAndSetObject(Object target, int i, Object exp, Object val)
   compareAndSetInt(Object target, int i, int exp, int val)
   compareAndSetLong(Object target, int i, long exp, long val)
This is a little delicate but ought to lead to better performance.
I'll try this out under openJDK. If successful, it may be time to
also allow people to evade ugly/painful AtomicXYUpdaters by
adding similar methods to java.lang.reflect.Field:
   compareAndSetObject(Object target, Object exp, Object val)
   compareAndSetInt(Object target, int exp, int val)
   compareAndSetLong(Object target, long exp, long val)
It will a few weeks at best before I can report on enough
experience to decide to argue for Java7 inclusion.

-Doug






More information about the Concurrency-interest mailing list