Java examples for Thread:ThreadLocalRandom
In JDK 7, java.util.concurrent includes a convenience class, ThreadLocalRandom.
It is for applications that expect to use random numbers from multiple threads or ForkJoinTasks.
For concurrent access, using ThreadLocalRandom instead of Math.random() results in less contention and, ultimately, better performance.
All you need to do is call ThreadLocalRandom.current(), then call one of its methods to retrieve a random number.
Here is one example:
import java.util.concurrent.ThreadLocalRandom; public class Main { public static void main(String[] args) throws InterruptedException { // have a look at other methods of the class... // ThreadLocalRandom.current(); while (true) { System.out.println("Int between 0 and 99: " + ThreadLocalRandom.current() .nextInt(0, 100)); Thread.sleep(1000);/* www .j a va 2 s . c o m*/ } } }