Java调度程序完全独立于系统时间的变化 [英] Java scheduler which is completely independent of system time changes

查看:172
本文介绍了Java调度程序完全独立于系统时间的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java Timer,然后切换到ScheduledExecutorService,但我的问题没有解决。
在系统时间更改之前安排的任务(通过ntpd)不会在指定的延迟时间执行。没有任何日志记录相同:(。

Was using Java Timer, Then switched to ScheduledExecutorService, but my problem is not fixed. As Tasks scheduled before system time change (through ntpd) are not executed on delay specified. Have no logs for same as nothing happens :(.

在我的目标64位linux上使用jre 1.6.0_26 64位。

using jre 1.6.0_26 64 bit in my target on 64 bit linux.


更新: ScheduledExecutorService在Windows上正常运行。问题仅出在64位基于Linux的系统上运行64位JVM。它工作正常
on
64位linux运行32位JVM ...奇怪。在任何博客上都没有找到任何相同的引用。

Update: ScheduledExecutorService works fine on Windows. Problem is only on 64 bit Linux based system running 64 bit JVM. It works fine on 64 bit linux running 32 bit JVM...strange. Have not found any reference of same on any blogs either.

IBM的JAVA SDK有同样的问题
(ibm-java-sdk-7.0-0.0-x86_64-archive.bin)。

IBM's JAVA SDK has same problem (ibm-java-sdk-7.0-0.0-x86_64-archive.bin).

我已经提交了针对JDK的缺陷7139684 ,已被接受,但已被关闭且
标记为6900441 。请投票支持,如果您觉得
值得修复它...我不知道为什么它超过几年没有修复

I had filed defect against JDK 7139684,It was accepted but has been closed and marked duplicate of 6900441. Please vote for it , if you feel its worth to get it fixed... I have no idea why its not been fixed since more than couple of years

以下是我用来测试此问题的示例代码:

Here is sample code I used to test this issue:

package test;

import java.io.IOException;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author yogesh
 *
 */
public class TimerCheck  implements Runnable {

    ScheduledExecutorService worker;


    public TimerCheck(ScheduledExecutorService worker) {
        super();
        this.worker = worker;
        this.worker.schedule(this, 1, TimeUnit.SECONDS);
    }

    private static void update() {
        System.out.println("TimerCheck.update() "+new Date(System.currentTimeMillis()));
    }

    @Override
    public void run() {
            update();
            worker.schedule(this, 1, TimeUnit.SECONDS);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        ScheduledExecutorService worker = Executors.newScheduledThreadPool(1);
        new TimerCheck(worker);
    }

}


推荐答案

在Sytem时间更改期间,有 JVM中的错误进行整体调度向后,这也影响到非常基本的Object.wait& Thread.sleep方法。当系统时间切换回甚至几秒钟时,保持Java App运行变得太冒险。你永远不知道你的Java应用程序最终会是什么。

There is bug in JVM for overall scheduling during Sytem time changes backward, which also impacts very basic Object.wait & Thread.sleep methods. It becomes too risky to keep Java App running when system time switches back by even certain seconds. You never know what your Java App will end up to.

所以我们决定:


  • 编写看门狗脚本(非Java :))来检查时间变化。

  • 如果时间切换回一定数量,关闭并重新启动Java应用程序。

另一种可能性是转向32位JVM,但我们使用的是JNI,然后在目标平台上使用的本机库不兼容32位。另外根据我们的经验,32位JVM将我们的目标限制在1.6G堆,这对我们来说根本不够。

Another possibility was to move to 32bit JVM, But we are using JNI and then native libraries used on target platform are not 32bit compatible. Also from our experience 32bit JVM limits our target to 1.6G heap, which is not at all sufficient for us.

我知道我们的解决方案不是最优雅,但直到JVM是固定的或找到一些更好的解决方案,似乎没有任何其他方式。

I know ours is not most elegant solution, but untill JVM is fixed or some better solution is found, there doesn't seem to be any other way.

编辑:
我们是除了上述解决方案之外,还考虑了克里斯的第一个建议:

Edited: We are also considering 1st suggestion from Chris in addition to above solution:


  • 配置NTP永远不会有大的时间跳跃。只能慢慢地花掉时间
    。只在停机期间手动应用大时间跳转。

这篇关于Java调度程序完全独立于系统时间的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆