O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required