How to do it...

First, we'll reproduce a problem caused by the concurrent modification of the same value. Let's create a Calculator class that has the calculate() method:

class Calculator {   private double prop;   public double calculate(int i){      DoubleStream.generate(new Random()::nextDouble).limit(50);      this.prop = 2.0 * i;      DoubleStream.generate(new Random()::nextDouble).limit(100);      return Math.sqrt(this.prop);   }}

This method assigns an input value to a property and then calculates its square root. We also inserted two lines of code that generate streams of 50 and 100 values. We did this to keep the method busy for some time. Otherwise, everything is done so quickly that there will be little chance for any concurrency to occur. Our adding the ...

Get Java 11 Cookbook 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.