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 to the nextInt() method passing the number 10 as the parameter. This method will return a pseudo random number between 0 and 10. Each task generates 10 random numbers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.216.175