2
我有一个ScheduledService,通过设置setPeriod(Duration.seconds((int) 1 + Math.rand()*100))
随机时间(从1秒到101秒)重新启动。如果随机数是65,那么服务系统地每65秒重新启动一次。javafx:如何设置ScheduledService以随机时间间隔重新启动?
但是,我真正想要的是它会在每个周期的随机(而不是固定)时间重新启动。
UPDATE:通过随机的,我的意思是它会产生每次运行的随机数。所以,也许该服务将在接下来的10秒内再次重启,下一次为100秒,下一次为35秒。
我该如何做到这一点?
class Foo
private final ScheduledService<Item> service = new ScheduledService<Item>() {
@Override
public Task<Item> createTask(){
return new Task<Item>() {
@Override
public Item call() throws Exception {
return //Item object
}
};
}
};
// constructor
public Foo(){
service.setPeriod(Duration.seconds((int) 1 + Math.rand()*100));
....
service.startMonitoring();
}
public final void startMonitoring() {
service.restart();
}
public final void stopMonitoring() {
service.cancel();
}
}