How it works...

The key of this example is in the TaskLocalRandom class. In the constructor of the class, we make a call to the current() method of the ThreadLocalRandom class. This is a static method that returns the ThreadLocalRandom object associated with the current thread, so you can generate random numbers using that object. If the thread that makes the call does not have any object associated yet, the class creates a new one. In this case, you use this method to initialize the random generator associated with this task, so it will be created in the next call to the method.

In the run() method of the TaskLocalRandom class, make a call to the current() method to get the random generator associated with this thread, also you make a call ...

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.