简体   繁体   中英

Schedule a task after inactivity (refreshable)

I'm looking for a way to schedule a task after some time, but i need to be able to delay the task if something happen. For example

myMethod() {
    some code...

    new Timer().schedule(new TimerTask(){
            @Override
            public void run(){
                LOGGER.info("close connexion");
            }
        },1000);

}

I need to reschedule the task in "run" if i come again in myMethod(). How to do?

Solution #1

Please see at ScheduledExecutorService that is available from java 1.5 It gives your method scheduleWithFixedDelay - so it re-schedules your task only after accomplishment. Another way to re-schedule task (with schedule method) right at the end of your method

Solution #2 - near the same signature of methods exists in the Spring TaskScheduler , but you can do it with help of annotated method of services.

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