问题
I am having an odd error with Thread.sleep()
on Java. For some reason, when I call sleep on some machines, it never returns. I can't figure out what could be causing this behaviour. At first, I thgouth the error might be elsewhere in my code, so I made the simplest possible sleep test:
public class SleepTest {
public static void main (String [] args) {
System.out.println ("Before sleep...");
try {
Thread.sleep (100);
} catch (InterruptedException e) {
}
System.out.println ("After sleep...");
}
}
On most machines it works, but on several machines which I am remotely logging into, it pauses indefinitely between the print statements. I have waited up to a half an hour with no change in behaviour. The machines that are displaying this error are Linux machines. Here is some information about the machines:
$ uname -a
Linux zone29ea 2.6.32-220.17.1.el6.x86_64 #1 SMP Tue May 15 17:16:46 CDT 2012 x86_64 x86_64 x86_64 GNU/Linux
$ java -version
java version "1.6.0_22"
OpenJDK Runtime Environment (IcedTea6 1.10.6) (rhel-1.43.1.10.6.el6_2-x86_64)
OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode)
What could be causing this behaviour?
UPDATE
Revised version, which still never ends:
public class SleepTest {
public static void main (String [] args) {
new Thread () {
public void run () {
System.out.println ("Before sleep...");
try {
Thread.sleep (100);
} catch (InterruptedException e) {
e.printStackTrace ();
}
System.out.println ("After sleep...");
}
}.start();
}
}
回答1:
if your server is running under Linux, you may be hit by the Leap Second bug which appears last week-end.
This bug affects the Linux kernel (the Thread management), so application which uses threads (as the JVM, mysql etc...) may consume high load of CPU.
回答2:
If your servers uses NTP (as you mentioned) and your CPU usage goes to 100%, check for Clock: inserting leap second 23:59:60 UTC
in your dmesg:
, if you find that, you are sure that your server affected with Leap Second bug
, unfortunately Java is the one which is most effected.
To resolve this, with out restarting any servers (like, tomcat) run the following commands.
/etc/init.d/ntp stop
date `date +"%m%d%H%M%C%y.%S"`
Hope this helps..
回答3:
This does seem to be leap second related.
Based on a post at from https://lkml.org/lkml/2012/7/1/19, I did:
date -s "`date`"
and it fixed the problem for me
来源:https://stackoverflow.com/questions/11294573/thread-sleep-never-returns