English 中文(简体)
java 任务时间表
原标题:java timer task schedule

从在Stack Overflow I ve读到,你们当中许多人没有建议使用时间任务。 Hmmm...,但我已经执行:

我有这一法典:

detectionHandlerTimer.schedule(myTimerTask, 60 * 1000, 60 * 1000);

事实是,我的时代者Task的工作持续了一段时间。

我希望这一行为:

  1. wait 60 sec.
  2. do task for some time (e.g. 40 - 100 sec).
  3. task finished.
  4. wait 60 seconds.
  5. do task for some time (e.g. 40 - 100 sec).

但是,上述法典就是这样。

  1. wait 60 sec.
  2. do task for some time (e.g. 40 - 100 sec).
  3. task finished
  4. do task for some time (e.g. 40 - 100 sec).

由于任务的时间长度大于60年,在任务结束后立即开始任务的时间更长。 但我要再次等待。

最佳回答

这一工作。 关键是要让任务本身(完成后)安排下一次任务。

import java.util.Timer;
import java.util.TimerTask;

public class TaskManager {

    private Timer timer = new Timer();

    public static void main(String[] args) {
        TaskManager manager = new TaskManager();
        manager.startTask();
    }

    public void startTask() {
        timer.schedule(new PeriodicTask(), 0);
    }

    private class PeriodicTask extends TimerTask {
        @Override
        public void run() {
            System.out.println(System.currentTimeMillis() + " Running");

            /* replace with the actual task */
            try {
                Thread.sleep(15 * 1000);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
            /* end task processing */

            System.out.println(System.currentTimeMillis() + " Scheduling 10 seconds from now");
            timer.schedule(new PeriodicTask(), 10 * 1000);
        }
    }
}

印刷:

$ javac TaskManager.java && java TaskManager
1288282514688 Running
1288282529711 Scheduling 10 seconds from now
1288282539712 Running
1288282554713 Scheduling 10 seconds from now
1288282564714 Running

在这里,如果你摘取时间档次的第二部分(清晰度):

$ javac TaskManager.java && java TaskManager
14                                    Running
29 (+15 seconds execution)            Scheduling 10 seconds from now
39 (+10 seconds delay until next run) Running
54 (+15 seconds execution)            Scheduling 10 seconds from now
64 (+10 seconds delay until next run) Running

仅取代<代码>10s>,改为60s。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签