日期:2014-05-20  浏览次数:20678 次

为什么每个线程的随机睡眠时间都相同,详见代码
如下代码,为什么每个线程的随机睡眠时间都相同,而且多次运行还是相同的睡眠时间,求解。

public class Sleep_ implements Runnable{
Random rand=new Random(47);
int i;
public void run(){
try{

i=rand.nextInt(1000);
TimeUnit.MILLISECONDS.sleep(i);
System.out.println("sleeep "+i+" milliseconds");
}catch(InterruptedException e){
System.err.println("Interrupted");
}
}

public static void main(String[] args){
ExecutorService exec=Executors.newCachedThreadPool();

for(int i=0;i<10;i++){
exec.execute(new Sleep_());
}
exec.shutdown();
}
}


------解决方案--------------------
Java code
Random rand=new Random(47);

------解决方案--------------------
探讨

Java code
Random rand=new Random(47);


换成:
Java code
Random rand=new Random();


试试。