Java thread sleeps a lot longer than specified

时间:2015-07-28 16:53:22

标签: java multithreading

I'm having an issue with Java Thread.sleep() on linux & JRE 1.7.

My monitoring application Thread has this source code:

public void run() {
    while(true) {  
        try {
            Thread.sleep(300000);
            logger.info("a");
        } catch (InterruptedException e) {
            break;
        } catch (Throwable ex) {
            logger.error(ex);
        }
        logger.info("...Runtime.getRuntime().freeMemory() in MB...");
    }        
}

My log file looks like this:

03:26:38,119  INFO : a
03:26:38,119  INFO : 9.67 MB
03:32:08,288  INFO : a
03:32:08,288  INFO : 8.90 MB
03:40:07,458  INFO : a
03:40:07,458  INFO : 8.00 MB
03:50:02,201  INFO : a
03:50:02,201  INFO : 7.17 MB
03:58:38,583  INFO : a
03:58:38,583  INFO : 6.27 MB
04:15:31,442  INFO : a
04:15:31,442  INFO : 5.32 MB
04:25:46,011  INFO : a
04:25:46,012  INFO : 4.56 MB
04:35:41,815  INFO : a
04:35:41,954  INFO : 3.63 MB
04:46:47,826  INFO : a
04:46:47,826  INFO : 2.71 MB
04:57:57,904  INFO : a
04:57:57,904  INFO : 1.95 MB
05:08:52,292  INFO : a
05:08:52,292  INFO : 10.35 MB
05:20:47,748  INFO : a
05:20:47,748  INFO : 9.38 MB
05:28:51,388  INFO : a
05:28:51,389  INFO : 8.63 MB
05:38:24,769  INFO : a
05:38:24,769  INFO : 7.73 MB
05:45:22,565  INFO : a
05:45:22,565  INFO : 6.77 MB
05:55:25,731  INFO : a
05:55:25,731  INFO : 5.99 MB
06:05:40,590  INFO : a
06:05:40,590  INFO : 5.09 MB
06:11:50,741  INFO : a
06:11:50,741  INFO : 4.19 MB
06:25:13,663  INFO : a
06:25:13,663  INFO : 3.28 MB

Do you have any ideas, how it is possible? Differences between log file entries are much larger, than thread.sleep parameter.

I don't know, what to do...

1 个答案:

答案 0 :(得分:0)

问题是由闰秒引起的,旧版本的linux内核带有这个bug会导致上面的任务调度问题......