< prev index next >

src/java.base/share/classes/java/util/concurrent/atomic/LongAccumulator.java

Print this page
8234131: Miscellaneous changes imported from jsr166 CVS 2020-06
Reviewed-by: martin


  91      * and identity element.
  92      * @param accumulatorFunction a side-effect-free function of two arguments
  93      * @param identity identity (initial value) for the accumulator function
  94      */
  95     public LongAccumulator(LongBinaryOperator accumulatorFunction,
  96                            long identity) {
  97         this.function = accumulatorFunction;
  98         base = this.identity = identity;
  99     }
 100 
 101     /**
 102      * Updates with the given value.
 103      *
 104      * @param x the value
 105      */
 106     public void accumulate(long x) {
 107         Cell[] cs; long b, v, r; int m; Cell c;
 108         if ((cs = cells) != null
 109             || ((r = function.applyAsLong(b = base, x)) != b
 110                 && !casBase(b, r))) {

 111             boolean uncontended = true;
 112             if (cs == null
 113                 || (m = cs.length - 1) < 0
 114                 || (c = cs[getProbe() & m]) == null
 115                 || !(uncontended =
 116                      (r = function.applyAsLong(v = c.value, x)) == v
 117                      || c.cas(v, r)))
 118                 longAccumulate(x, function, uncontended);
 119         }
 120     }
 121 
 122     /**
 123      * Returns the current value.  The returned value is <em>NOT</em>
 124      * an atomic snapshot; invocation in the absence of concurrent
 125      * updates returns an accurate result, but concurrent updates that
 126      * occur while the value is being calculated might not be
 127      * incorporated.
 128      *
 129      * @return the current value
 130      */
 131     public long get() {
 132         Cell[] cs = cells;
 133         long result = base;
 134         if (cs != null) {
 135             for (Cell c : cs)
 136                 if (c != null)
 137                     result = function.applyAsLong(result, c.value);
 138         }




  91      * and identity element.
  92      * @param accumulatorFunction a side-effect-free function of two arguments
  93      * @param identity identity (initial value) for the accumulator function
  94      */
  95     public LongAccumulator(LongBinaryOperator accumulatorFunction,
  96                            long identity) {
  97         this.function = accumulatorFunction;
  98         base = this.identity = identity;
  99     }
 100 
 101     /**
 102      * Updates with the given value.
 103      *
 104      * @param x the value
 105      */
 106     public void accumulate(long x) {
 107         Cell[] cs; long b, v, r; int m; Cell c;
 108         if ((cs = cells) != null
 109             || ((r = function.applyAsLong(b = base, x)) != b
 110                 && !casBase(b, r))) {
 111             int index = getProbe();
 112             boolean uncontended = true;
 113             if (cs == null
 114                 || (m = cs.length - 1) < 0
 115                 || (c = cs[index & m]) == null
 116                 || !(uncontended =
 117                      (r = function.applyAsLong(v = c.value, x)) == v
 118                      || c.cas(v, r)))
 119                 longAccumulate(x, function, uncontended, index);
 120         }
 121     }
 122 
 123     /**
 124      * Returns the current value.  The returned value is <em>NOT</em>
 125      * an atomic snapshot; invocation in the absence of concurrent
 126      * updates returns an accurate result, but concurrent updates that
 127      * occur while the value is being calculated might not be
 128      * incorporated.
 129      *
 130      * @return the current value
 131      */
 132     public long get() {
 133         Cell[] cs = cells;
 134         long result = base;
 135         if (cs != null) {
 136             for (Cell c : cs)
 137                 if (c != null)
 138                     result = function.applyAsLong(result, c.value);
 139         }


< prev index next >