简体   繁体   中英

Java TimerTask using Timer#schedule never runs

I'm trying to get a Java (1.7) repeating TimerTask to work eg

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

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 . I'm using log4j but I'm using it exactly as I do in every other class and it works fine. 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.

Look at the method Timer#schedule(TimerTask task,long delay,long period) that you have used of Timer class.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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