Randomizing Thread sleep

perfphysio

New Member
I have the following code within the run method of a runnable:\[code\]@Overridepublic 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.tryAcquire()){ System.out.println("Acquired for " + i); Thread.sleep(r.nextInt()); sems.increment(); sems.release(); acquired=1; break; } } } } catch(InterruptedException x){}}\[/code\]I keep getting the following exception a few moments into the execution:\[code\]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)\[/code\]However,if I use a Thread.sleep(1000) the program runs fine.Why am I not able to randomize the pause using java Random?
 
Top