SingleThreadScheduledExecutor运行计划

时间:2018-01-04 14:38:51

标签: java for-loop scheduledexecutorservice

我有一个包含singleThreadScheduledExecutor的run方法。目标是在白天每隔30分钟运行一次,从早上6点到晚上8点。如果当天是在周末或那些时间之外,运行时程序应该在日志中添加一条消息,并等待再过30分钟再试一次。

然而,当服务器启动我的程序并且它在正确的时间范围内时(在这种情况下,假设它试图在早上7点第一次运行程序)当它超过指定的时间时它仍将运行(在这种情况下,它会在晚上8:30再次尝试,但仍然会运行)。通过它进行调试会通过if语句而忽略大小写。我还发现,如果我在正确的小时之外运行我的程序,它会在日志中添加一条错误消息,但是当它返回到正确的时间范围内时,它将继续只记录错误而不是运行程序

我的问题是:我在逻辑中错过了一些我根本没看到的东西吗?有没有更好/更有效的方法来做到这一点?

提前致谢。

以下是代码

public final void run() {
    final Calendar cal = Calendar.getInstance();
    exec = Executors.newSingleThreadScheduledExecutor();
    exec.scheduleAtFixedRate(new Runnable() {
        public void run() {
            try {

                if (cal.get(Calendar.DAY_OF_WEEK) == 1 || cal.get(Calendar.DAY_OF_WEEK) == 7) {
                    Globals.addMessage("Non-scheduled work day, please try again later.");
                    Globals.addMessage("Process aborted.");
                    running = false;

                }
                else if (cal.get(Calendar.HOUR_OF_DAY) < 6 || cal.get(Calendar.HOUR_OF_DAY) > 20) {
                    Globals.addMessage("Non-scheduled work hours, please try again later.");
                    Globals.addMessage("Process aborted.");
                    running = false;

                }
                else {
                    running = true;
                    Runtime rt = Runtime.getRuntime();
                    rt.exec(ServerVar.getVariable(ServerVars.PATH));
                    Globals.addMessage("Program is running.");
                }
            }
            catch (IOException e) {
                Globals.addMessage(e.toString());

            }

        }
    }

        , 0, 30, TimeUnit.MINUTES);
}

1 个答案:

答案 0 :(得分:0)

您总是使用相同的Calendar。试试这个:

public final void run() {
    exec = Executors.newSingleThreadScheduledExecutor();
    exec.scheduleAtFixedRate(new Runnable() {
        public void run() {
            final Calendar cal = Calendar.getInstance();
            try {

                if (cal.get(Calendar.DAY_OF_WEEK) == 1 || cal.get(Calendar.DAY_OF_WEEK) == 7) {
                    Globals.addMessage("Non-scheduled work day, please try again later.");
                    Globals.addMessage("Process aborted.");
                    running = false;

                }
                else if (cal.get(Calendar.HOUR_OF_DAY) < 6 || cal.get(Calendar.HOUR_OF_DAY) > 20) {
                    Globals.addMessage("Non-scheduled work hours, please try again later.");
                    Globals.addMessage("Process aborted.");
                    running = false;

                }
                else {
                    running = true;
                    Runtime rt = Runtime.getRuntime();
                    rt.exec(ServerVar.getVariable(ServerVars.PATH));
                    Globals.addMessage("Program is running.");
                }
            }
            catch (IOException e) {
                Globals.addMessage(e.toString());

            }

        }
    }

        , 0, 30, TimeUnit.MINUTES);
}