[英]Reinitialize fix delay in ScheduledExecutorService
根據我的要求,我必須在一段時間后執行一些特定的代碼。 為了做到這一點,我選擇了ScheduledExecutorService.scheduleWithFixedDelay(runnable, 0, 5, TimeUnit.SECONDS)
,它對我來說很好。 但根據我的另一個要求, fixedDelay
提到的時間應該可以在運行時配置。 手段,當前總延遲是5秒,但是后者如果用戶想要則可以將時間更改為60秒,並且在運行時, fixedDelay
將在60秒后運行。 任何幫助都會很明顯。
請看代碼:
static int i = 0;
static ScheduledExecutorService executor;
static Runnable runnable;
static ScheduledFuture<?> future;
public static void main(String args[]) {
executor = Executors
.newScheduledThreadPool(1);
runnable = new Runnable() {
@Override
public void run() {
System.out.println("Inside runnable" + i++);
changeDelay();
}
};
future =
executor.scheduleWithFixedDelay(runnable, 0, 5, TimeUnit.SECONDS);
}
public static void changeDelay() {
future.cancel(false);
future = executor.scheduleWithFixedDelay(runnable, 0, 10, TimeUnit.SECONDS);
}
這里我使用changeDelay方法來改變延遲時間。 但它不起作用。
您需要保留返回的ScheduledFuture<?>
對象的引用:
ScheduledFuture<?> handle =
scheduler.scheduleWithFixedDelay(runnable, 0, 5, TimeUnit.SECONDS);
使用此引用,您將取消當前任務並使用新延遲創建另一個任務:
handle.cancel(false);
handle = scheduler.scheduleWithFixedDelay(runnable, 0, 60, TimeUnit.SECONDS);
這是一個例子:
public class Test5 {
static int i = 0;
static ScheduledExecutorService executor;
static Runnable runnable;
static ScheduledFuture<?> future;
public static void main(String args[]) throws InterruptedException{
executor = Executors.newScheduledThreadPool(1);
runnable = new Runnable() {
@Override
public void run() {
System.out.println("Inside runnable" + i++);
}
};
future = executor.scheduleWithFixedDelay(runnable, 0, 5, TimeUnit.SECONDS);
Thread.sleep(20000l);
changeDelay();
}
public static void changeDelay() {
boolean res = future.cancel(false);
System.out.println("Previous task canceled: " + res);
future = executor.scheduleWithFixedDelay(runnable, 0, 20, TimeUnit.SECONDS);
}
}
正如我所見, ScheduledExecutorService.scheduleWithFixedDelay(runnable, initDelay , delay, TimeUnit.SECONDS)
返回ScheduledFuture
, 它沒有方法setDelay(delay)
,這將允許它在創建后更改延遲 。
最好的辦法是取消當前運行的ScheduledFuture.cancel(false)
,並開始用的幫助下,新的計划任務scheduleWithFixedDelay
使用新的延遲值。
請勿從run
方法調用 changeDelay
。 而是從main
方法本身調用它,如下所示
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class ScheduledExecutorExample {
static int i = 0;
static ScheduledExecutorService executor;
static Runnable runnable;
static ScheduledFuture<?> future;
public static void main(String args[]) {
executor = Executors.newScheduledThreadPool(1);
runnable = new Runnable() {
@Override
public void run() {
System.out.println("Inside runnable" + i++);
}
};
future = executor.scheduleWithFixedDelay(runnable, 0, 2, TimeUnit.SECONDS);
try {
Thread.sleep(4000); //sleeping for 2 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
changeDelay();
}
public static void changeDelay() {
future.cancel(false);
future = executor.scheduleWithFixedDelay(runnable, 0, 10, TimeUnit.SECONDS);
}
}
你看到一段時間后延遲從2秒變為10秒
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.