总结 处理java.lang.IllegalArgumentException: timeout value is negative 异常的关键在于找到并修正导致超时值为负的代码部分。这通常涉及到检查硬编码的值、计算逻辑以及参数传递的正确性。通过合理的代码审查和测试,可以有效地避免这类异常的发生。
首先这个参数是必须>=0的,如果输入负数会抛出异常:java.lang.IllegalArgumentException: timeout value is negative 再看看源码中这个方法的注释 Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accur...
long base = System.currentTimeMillis(); long now = 0; if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } //millis等于0 表示无限期等待 if (millis == 0) { while (isAlive()) { //调用wait方法 wait(0); } } //否则,限时等待 else { while (isAli...
这里数据的校验在jvm层逻辑中校验 if (millis < 0) { THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative"); } // 如果线程已经被打断了,那么也抛出异常 if (Thread::is_interrupted (THREAD, true) && !HAS_PENDING_EXCEPTION) { THROW_MSG(vmSymbols::java_lang_...
publicfinalvoidwait(longtimeout,intnanos)throwsInterruptedException {if(timeout <0) {thrownewIllegalArgumentException("timeout value is negative");}if(nanos <0|| nanos >999999) {thrownewIllegalArgumentException("nanosecond timeout value out of range");}if(nanos >0) {timeout++;}wait(timeout)...
throw new IllegalArgumentException("timeout value is negative"); } if (millis == 0) { while (isAlive()) { wait(0); } } else { while (isAlive()) { long delay = millis - now; if (delay <= 0) { break; } wait(delay); ...
wait(long timeout, int nanos)方法: public final void wait(long timeout, int nanos) throws InterruptedException { if (timeout < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (nanos < 0 || nanos > 999999) { ...
wait(longtimeout,longnanos)本质上还是调用一个参数的方法publicfinalvoidwait(longtimeout,intnanos)throwsInterruptedException{if(timeout <0) {thrownewIllegalArgumentException("timeout value is negative");}if(nanos <0|| nanos >999999) {thrownewIllegalArgumentException("nanosecond timeout value out of ...
//本地方法,真正让线程休眠的方法 public static native void sleep(long millis) throws InterruptedException; public static void sleep(long millis, int nanos) throws InterruptedException { if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (nanos < 0 || nano...
throw new IllegalArgumentException("timeout value is negative"); } if (millis == 0) { while (isAlive()) { wait(0); } } else { while (isAlive()) { long delay = millis - now; if (delay <= 0) { break; } wait(delay); ...