Skip to content

Commit 9322a84

Browse files
committed
Issue google#735: fixed hang calling Thread.sleep(0).
Change on 2016/04/01 by tball <[email protected]> ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=118803764
1 parent 7902297 commit 9322a84

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

jre_emul/Classes/java/lang/Thread.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -798,10 +798,30 @@ public static void sleep(long millis) throws InterruptedException {
798798
}
799799

800800
public static void sleep(long millis, int nanos) throws InterruptedException {
801-
Object lock = currentThread().vmThread;
802-
synchronized(lock) {
803-
lock.wait(millis, nanos);
804-
}
801+
if (millis < 0) {
802+
throw new IllegalArgumentException("millis < 0: " + millis);
803+
}
804+
if (nanos < 0) {
805+
throw new IllegalArgumentException("nanos < 0: " + nanos);
806+
}
807+
if (nanos > 999999) {
808+
throw new IllegalArgumentException("nanos > 999999: " + nanos);
809+
}
810+
811+
// The JLS 3rd edition, section 17.9 says: "...sleep for zero
812+
// time...need not have observable effects."
813+
if (millis == 0 && nanos == 0) {
814+
// ...but we still have to handle being interrupted.
815+
if (Thread.interrupted()) {
816+
throw new InterruptedException();
817+
}
818+
return;
819+
}
820+
821+
Object lock = currentThread().vmThread;
822+
synchronized(lock) {
823+
lock.wait(millis, nanos);
824+
}
805825
}
806826

807827
/**

0 commit comments

Comments
 (0)