Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread.sleep() Never Returns

Tags:

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();
    }
}
like image 499
101100 Avatar asked Jul 02 '12 13:07

101100


People also ask

What is the disadvantage of thread sleep?

Thread. sleep is bad! It blocks the current thread and renders it unusable for further work.

Do you use thread sleep () frequently?

You need to write sleep() method whenever we need to make webdriver wait. So if you want to wait for two web elements, you need to write Thread. sleep() twice just before you locate web elements. It is not good programming practice.

What's thread sleep () in threading?

Suspends the current thread for the specified number of milliseconds. Suspends the current thread for the specified amount of time.

Does thread blocking sleep?

Sleep method. Calling the Thread. Sleep method causes the current thread to immediately block for the number of milliseconds or the time interval you pass to the method, and yields the remainder of its time slice to another thread. Once that interval elapses, the sleeping thread resumes execution.


2 Answers

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.

like image 145
Jean-Philippe Briend Avatar answered Sep 23 '22 01:09

Jean-Philippe Briend


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..

like image 26
RP- Avatar answered Sep 25 '22 01:09

RP-