![](/img/trans.png)
[英]How do I make Swagger-UI use a YAML/JSON rather than having to put annotations on my REST controller?
[英]How do i put a limit on TimerTask rather than having it run forever?
我正在使用计时器实用程序来重复发布代码的输出。 现在,我将其设置为以1秒的间隔重复不间断。 我该如何更改它以重复执行直到达到用户设置的时间限制?
//Create a new timer
Timer timer = new Timer();
static int interval = 1000; //Interval of timer in milliseconds
//Set the timer to start in 1 second and to go every certain amount of milliseconds.
timer.scheduleAtFixedRate(TimerClass.timertask, 1000, interval);
正如MadProgrammer所说; 这可以通过使用来实现TimerTask.cancel
方法从内TimerTask.run
方法。
例如:
public class RunUntilInstantTimerTask extends TimerTask {
private final Instant stopTime;
public RunUntilInstantTimerTask(final Instant stopTime) {
this.stopTime = Objects.requireNonNull(stopTime);
}
@Override
public void run() {
// Do scheduled stuff...
if (Instant.now().isAfter(this.stopTime)) {
this.cancel();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.