2

Some time ago I started to investigate java.util.concurrent package. And my question is about AtomicStampedReference class. The class has method

public V get(int[] stampHolder) {
    ...
}

which gets reference and stamp atomically (please, correct me if I'm wrong).

Is there some special reasons why parameter for the method is array? Documentation says nothing about it.

1 Answers1

2

Maybe (probably) it is just a hack to simulate out parameters, since in this case two values should be returned: V and an int value.

Another possibility would be to return a tuple Tuple<V,Integer>, but Java has no Tuple class, and it would maybe be less efficient to create an instance of a Tuple object and additionally boxing the int, which may be important in the context of concurrency.

Community
  • 1
  • 1
user140547
  • 7,750
  • 3
  • 28
  • 80
  • It is clear that actually we get two fresh values. But why did mr. Doug Lea decide to use `int[] stampHolder` instead of using `int stampHolder` for example? This method uses only the first array element - `stampHolder[0] = p.integer`. – Dmitriy Protsenko Sep 30 '16 at 07:42
  • @DmitriyProtsenko: Well if the method was defined as `public V get(int stampHolder)` if `stampHolder` is modified the caller won't get the updated value since it is passed by value. see http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – user140547 Sep 30 '16 at 08:30
  • The last link is very useful. Now I don't have any questions. Thanks! – Dmitriy Protsenko Sep 30 '16 at 09:12