[英]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(). 如果我再次进入myMethod(),则需要在“运行”中重新计划任务。 How to do?
怎么做?
Solution #1 解决方案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. 请参阅java 1.5上提供的ScheduledExecutorService ,它为您的方法
scheduleWithFixedDelay
因此它仅在完成后才重新计划您的任务。 Another way to re-schedule task (with schedule
method) right at the end of your method 在方法结束时重新安排任务的另一种方法(使用
schedule
方法)
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. 解决方案#2-Spring TaskScheduler中几乎存在相同的方法签名,但是您可以在带注释的服务方法的帮助下进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.