[concurrency-interest] Array element compareAndSet

Doug Lea dl at cs.oswego.edu
Wed Dec 17 18:59:42 EST 2008

Roman Elizarov wrote:
> DL> It seems that a better way to do this is to add three
> DL> methods to java.lang.reflect.Array:
> DL>    compareAndSetObject(Object target, int i, Object exp, Object val)
> DL>    compareAndSetInt(Object target, int i, int exp, int val)
> DL>    compareAndSetLong(Object target, int i, long exp, long val)
> How about volatile get/set methods? You could change
> Array.getInt/setInt to work as volatile and introduce lazySet/eagerGet

More likely, keeping existing ones as-is, and adding
   Object atomicGet(Object target, int i);
   void atomicSet(Object target, int i, Object val)
   void atomicLazySet(Object target, int i, Object val)

> Btw, it is strange, that java.lang.reflect.Array class is still
> written via native methods instead of sun.mics.Unsafe, so it is hard
> to see what it really does inside. The actual implementation is quite
> scary performance-wise.

The idea is to subject these to the dynamic compilation
strategy used for java.lang.reflect.Field methods, so that the
type checks can then in turn be subject to the usual optimizations.
This is probably the only path to reliably removing unnecessary
checks. But it will be a lot of work. If anyone else out there is
interested in taking this on, please feel free.


More information about the Concurrency-interest mailing list