我在可运行的运行方法中有以下代码:
@Override
public void run(){
final Random r=new Random(1000);
int acquired=0;
try{
while(acquired < 1){
for(int i=0;i<sems.length;i++){
if(sems[i].tryAcquire()){
System.out.println("Acquired for " + i);
Thread.sleep(r.nextInt());
sems[i].increment();
sems[i].release();
acquired=1;
break;
}
}
}
}
catch(InterruptedException x){}
}
在执行过程中,我不断收到以下异常:
Exception in thread "Thread-2" java.lang.IllegalArgumentException: timeout value is negative
at java.lang.Thread.sleep(Native Method)
at com.bac.jp.fourteenth$1.run(fourteenth.java:24)
at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-0" java.lang.IllegalArgumentException: timeout value is negative
at java.lang.Thread.sleep(Native Method)
at com.bac.jp.fourteenth$1.run(fourteenth.java:24)
at java.lang.Thread.run(Unknown Source)
但是,如果我使用 Thread.sleep(1000) 程序运行良好。为什么我不能使用 java Random 随机化暂停?