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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.