How to do it...

Follow these steps to implement the example:

  1. Create a class named Account to simulate a bank account:
        public class Account {
  1. 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;
  1. 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(); ...

Get Java 9 Concurrency Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.