在jdk7中,Java.util.concurrent包括了一个便利类,ThreadLocalRandom,用于那些期望从多线程或者ForkJoinTasks使用随机数的应用。
对于并发访问,使用ThreadLocalRandom 代替Math.random()带来了较少的连接,最终是更好的饿表现。
所有你需要做的是调用ThreadLocalRandom.current(),之后调用它的方法中的一个取回随机数。这里是一个例子:
int r = ThreadLocalRandom.current() .nextInt(4, 77);
对于并发访问,使用ThreadLocalRandom 代替Math.random()带来了较少的连接,最终是更好的饿表现。
所有你需要做的是调用ThreadLocalRandom.current(),之后调用它的方法中的一个取回随机数。这里是一个例子:
int r = ThreadLocalRandom.current() .nextInt(4, 77);