简体   繁体   English

使用Timer#schedule的Java TimerTask永远不会运行

[英]Java TimerTask using Timer#schedule never runs

I'm trying to get a Java (1.7) repeating TimerTask to work eg 我正在尝试让Java(1.7)重复TimerTask工作,例如

TokenJanitor janitor = new TokenJanitor( TokenManager.getInstance() );
try {
    new Timer().schedule(janitor, System.currentTimeMillis()+15000, 15000);
    LOG.info("TokenJanitor is running...");
    ...

And then the TokenJanitor is a TimerTask eg 然后TokenJanitor是一个TimerTask例如

public class TokenJanitor extends TimerTask {

    @Override
    public void run() {
        LOG.info("Running TokenJanitor cleanup now...");
        System.out.println("Hello..."); // just for good measure

It doesn't even run once. 它甚至没有运行一次。 I see the log message after the schedule function is called, but I can't see any log messages or print output from the TimerTask . 我在调用schedule函数后看到了日志消息,但是我看不到TimerTask任何日志消息或打印输出。 I'm using log4j but I'm using it exactly as I do in every other class and it works fine. 我正在使用log4j,但我正在使用它,就像我在其他所有类中一样,它工作正常。 For what it's worth, I'm using Jetty to run a web server, so the schedule function above is called in the main method of a class that launches the server. 为了它的价值,我使用Jetty来运行Web服务器,因此上面的schedule函数在启动服务器的类的main方法中调用。

Look at the method Timer#schedule(TimerTask task,long delay,long period) that you have used of Timer class. 查看您使用Timer类的Timer#schedule(TimerTask任务,长延迟,长时间段)方法

task - task to be scheduled.
delay - delay in milliseconds before task is to be executed.
period - time in milliseconds between successive task executions. 

you have specified System.currentTimeMillis()+15000 as delay that is very long time to run the task. 您已将System.currentTimeMillis()+15000指定为延迟,这是运行任务的非常长的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM