Java 7:ThreadLocalRandom 生成相同的随机数
我正在尝试Java 7的ThreadLocalRandom,看到它在多个线程中生成完全相同的随机数。
这是我的代码,其中我创建了5个线程,每个线程打印出5个随机数:
//5 threads
for(int i = 0; i < 5 ; i++) {
final Thread thread = new Thread() {
@Override
public void run() {
System.out.print(Thread.currentThread().getName()+":");
//each thread prints 5 random numbers
for(int j = 0 ; j < 5; j++) {
final int random = ThreadLocalRandom.current().nextInt(1,100);
System.out.print(random + ",");
}
System.out.println();
}
};
thread.start();
thread.join();
}
输出:
Thread-0:1,93,45,75,29,
Thread-1:1,93,45,75,29,
Thread-2:1,93,45,75,29,
Thread-3:1,93,45,75,29,
Thread-4:1,93,45,75,29,
为什么我为每个线程和每次程序执行都获得相同的随机数?