< prev index next >

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

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


  68  * @since 1.8
  69  * @author Doug Lea
  70  */
  71 public class LongAdder extends Striped64 implements Serializable {
  72     private static final long serialVersionUID = 7249069246863182397L;
  73 
  74     /**
  75      * Creates a new adder with initial sum of zero.
  76      */
  77     public LongAdder() {
  78     }
  79 
  80     /**
  81      * Adds the given value.
  82      *
  83      * @param x the value to add
  84      */
  85     public void add(long x) {
  86         Cell[] cs; long b, v; int m; Cell c;
  87         if ((cs = cells) != null || !casBase(b = base, b + x)) {

  88             boolean uncontended = true;
  89             if (cs == null || (m = cs.length - 1) < 0 ||
  90                 (c = cs[getProbe() & m]) == null ||
  91                 !(uncontended = c.cas(v = c.value, v + x)))
  92                 longAccumulate(x, null, uncontended);
  93         }
  94     }
  95 
  96     /**
  97      * Equivalent to {@code add(1)}.
  98      */
  99     public void increment() {
 100         add(1L);
 101     }
 102 
 103     /**
 104      * Equivalent to {@code add(-1)}.
 105      */
 106     public void decrement() {
 107         add(-1L);
 108     }
 109 
 110     /**
 111      * Returns the current sum.  The returned value is <em>NOT</em> an
 112      * atomic snapshot; invocation in the absence of concurrent




  68  * @since 1.8
  69  * @author Doug Lea
  70  */
  71 public class LongAdder extends Striped64 implements Serializable {
  72     private static final long serialVersionUID = 7249069246863182397L;
  73 
  74     /**
  75      * Creates a new adder with initial sum of zero.
  76      */
  77     public LongAdder() {
  78     }
  79 
  80     /**
  81      * Adds the given value.
  82      *
  83      * @param x the value to add
  84      */
  85     public void add(long x) {
  86         Cell[] cs; long b, v; int m; Cell c;
  87         if ((cs = cells) != null || !casBase(b = base, b + x)) {
  88             int index = getProbe();
  89             boolean uncontended = true;
  90             if (cs == null || (m = cs.length - 1) < 0 ||
  91                 (c = cs[index & m]) == null ||
  92                 !(uncontended = c.cas(v = c.value, v + x)))
  93                 longAccumulate(x, null, uncontended, index);
  94         }
  95     }
  96 
  97     /**
  98      * Equivalent to {@code add(1)}.
  99      */
 100     public void increment() {
 101         add(1L);
 102     }
 103 
 104     /**
 105      * Equivalent to {@code add(-1)}.
 106      */
 107     public void decrement() {
 108         add(-1L);
 109     }
 110 
 111     /**
 112      * Returns the current sum.  The returned value is <em>NOT</em> an
 113      * atomic snapshot; invocation in the absence of concurrent


< prev index next >