ScheduledExecutorService 将执行,而不管之前的任务完成状态如何
ScheduledExecutorService will execute irrespective of previous task completion status
我正在使用 ScheduledExecutorService
,有时任务可能 运行 3 小时才能完成 pollMergeFiles.searchForFileAndExecute()
方法,有时可能需要不到 2 分钟才能完成。
我唯一的问题是 scheduler.scheduleAtFixedRate
是否会每 10 分钟执行一次并延迟 5 分钟,或者它会等到上一个任务 运行 完成才开始新任务?
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
...
PollMergeFiles pollMergeFiles = new PollMergeFiles();
final Runnable service = new Runnable() {
public void run() {
try {
if (counter <= 256) {
pollMergeFiles.searchForFileAndExecute();
} else if (counter > 256) {
logger.info(
"Shutdown the scheduler service since all counters were processed");
scheduler.shutdown();
}
} catch (Exception e) {
logger.error("Exception found", e);
}
}
};
scheduler.scheduleAtFixedRate(service, 5, 10, TimeUnit.Minutes);
您可以查看 Java 文档 https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html .
scheduleAtFixedRate(Runnable command, long initialDelay, long period,
TimeUnit unit) Creates and executes a periodic action that becomes
enabled first after the given initial delay, and subsequently with the
given period; that is executions will commence after initialDelay then
initialDelay+period, then initialDelay + 2 * period, and so on.
所以 scheduleAtFixedRate() 不会等待最后一个任务完成。它将以预定义的时间间隔(周期字段)执行。
scheduleWithFixedDelay(Runnable command, long initialDelay, long
delay, TimeUnit unit) Creates and executes a periodic action that
becomes enabled first after the given initial delay, and subsequently
with the given delay between the termination of one execution and the
commencement of the next.
但是 scheduleWithFixedDelay() 方法可以在最后一个任务执行后等待一个预定义的时间(延迟字段)。
我正在使用 ScheduledExecutorService
,有时任务可能 运行 3 小时才能完成 pollMergeFiles.searchForFileAndExecute()
方法,有时可能需要不到 2 分钟才能完成。
我唯一的问题是 scheduler.scheduleAtFixedRate
是否会每 10 分钟执行一次并延迟 5 分钟,或者它会等到上一个任务 运行 完成才开始新任务?
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
...
PollMergeFiles pollMergeFiles = new PollMergeFiles();
final Runnable service = new Runnable() {
public void run() {
try {
if (counter <= 256) {
pollMergeFiles.searchForFileAndExecute();
} else if (counter > 256) {
logger.info(
"Shutdown the scheduler service since all counters were processed");
scheduler.shutdown();
}
} catch (Exception e) {
logger.error("Exception found", e);
}
}
};
scheduler.scheduleAtFixedRate(service, 5, 10, TimeUnit.Minutes);
您可以查看 Java 文档 https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html .
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on.
所以 scheduleAtFixedRate() 不会等待最后一个任务完成。它将以预定义的时间间隔(周期字段)执行。
scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.
但是 scheduleWithFixedDelay() 方法可以在最后一个任务执行后等待一个预定义的时间(延迟字段)。