[concurrency-interest] MORE ON: Re[2]: RFC -- Java7 java.util.concurrent plans

Roman Elizarov elizarov at devexperts.com
Thu Dec 11 07:13:24 EST 2008


Hello Doug!

I have to correct a statement from my previous email:

> * Constuructors for this classes should not be exposed at all. I
> would prefer a singleton pattern with a static generic "getInstance"
> method. Not only this is cleaner (each user does not define its own
> instance), but it also integrates with Java generic type inference.

Unfortunately, getInstance method does not solve inferrence problem,
so this AtomicReferenceArrayUpdater is bound to be hard to use in
practice. The fix is to make all methods static, but then it does not
make sense to keep those methods in this class. 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".

It would be utterly cool for certain algorithms if this class also
contains a method with the following signature:
  void set(int[] a, int index, int value0, int value1);
which atomically does:
  a[index] = value0;
  a[index+1] = value1;

now, the paired get for this would look ugly, so it might be a better
aproach to pack the pair into long from the very start:
  void setLittleEndian(int[] a, int index, long value);
  void setBigEndian(int[] a, int index, long value);
  long getLittleEndian(int[] a, int index);
  long getBigEndian(int[] a, int index);

// or figure out a better set of suffixes than BigEndian/LittleEndian

This will close yet another Java vs native performance gap. Actually,
there are only few places left where Java algorithm running on HotSpot
significantly underperform compared to C/C++. One place is related to
high-performance/concurrent data strucutures. Consider a hash map from
int to Object. In C++ you would implement it as an array of structs.
But in Java the only way to implement it is to have a separate int[]
and a separate Object[]. Makes it twice as slow in practice, since
there are twice as many cache rows to load. We badly need
heterogenous/struct arrays. How pity that this complain should go to
some other place....

Sincerely,
Roman Elizarov




More information about the Concurrency-interest mailing list