Skip to content
Related Articles

Related Articles

Improve Article

LongAccumulator getThenReset() method in Java with Examples

  • Last Updated : 28 Jan, 2019

The java.LongAccumulator.getThenReset() is an inbuilt method in java that is equivalent in effect to get() followed by reset(). Firstly, it gets the current value and then resets the value.

Syntax:

public long getThenReset()

Parameters: This method does not accepts any parameter.

Return Value: This method returns the value before reset.

Below programs illustrate the above method:



Program 1:




// Program to demonstrate the getThenReset() method
  
import java.lang.*;
import java.util.concurrent.atomic.LongAccumulator;
  
public class GFG {
    public static void main(String args[])
    {
        LongAccumulator num = new LongAccumulator(Long::sum, 0L);
  
        // accumulate operation on num
        num.accumulate(42);
        num.accumulate(10);
  
        num.get();
        // before getThenReset the value is
        System.out.println(" the old value is: " + num);
        ;
  
        // getThenResets current value
        num.getThenReset();
  
        // Print after getThenReset operation
        System.out.println(" the current value is: " + num);
    }
}
Output:
the old value is: 52
 the current value is: 0

Program 2:




// Program to demonstrate the getThenReset() method
  
import java.lang.*;
import java.util.concurrent.atomic.LongAccumulator;
  
public class GFG {
    public static void main(String args[])
    {
        LongAccumulator num = new LongAccumulator(Long::sum, 0L);
  
        // accumulate operation on num
        num.accumulate(2);
        num.accumulate(1);
  
        num.get();
        // before getThenReset the value is
        System.out.println(" the old value is: " + num);
          
  
        // getThenResets current value
        num.getThenReset();
  
        // Print after getThenReset operation
        System.out.println(" the current value is: " + num);
    }
}
Output:
the old value is: 3
the current value is: 0

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/LongAccumulator.html#getThenReset–




My Personal Notes arrow_drop_up
Recommended Articles
Page :