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 it works...

The key to this example is in the Account class. In this class, we declared an AtomicLong variable named balance to store the balance of the account, a LongAdder variable named operations to store the number of operations we made with the account, and a DoubleAccumulator variable named commission to store the value of the commissions of the operations. In the constructor of the commission object, we specified that the value will be incremented with the expression 0.2*y. With this, we wanted to specify that we will increment the actual value of the variable with the result of its multiplication by 0.2 and the value of the parameter we pass to the accumulate() method.

To implement the getBalance() method that returns the value ...

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