Follow these steps to implement the example:
- Create a class named Account to simulate a bank account:
public class Account {
- Declare a private AtomicLong attribute named balance to store the balance of the account. In addition, declare a private LongAdder attribute named operations and a private DoubleAccumulator attribute named commission:
private final AtomicLong balance; private final LongAdder operations; private final DoubleAccumulator commission;
- Implement the constructor of the class to initialize its attributes. For the DoubleAccumulator class, the identity value is 0 and we update the actual value with the result of multiply 0.2 to the increment passed as parameter:
public Account() { balance = new AtomicLong(); ...