更改线程池中一个线程的延迟
Change delay of one thread in threadpool
我向我的线程池添加了三个具有不同任务和不同延迟的线程,并且一切正常。
static ScheduledExecutorService scheduleTaskExecutor;
static ScheduledFuture<?> future1;
static ScheduledFuture<?> future2;
static ScheduledFuture<?> future3;
public void onCreate(Bundle savedInstanceState) {
scheduleTaskExecutor = Executors.newScheduledThreadPool(3);
future1 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable1() {...}, 0, olddelay1, TimeUnit.SECONDS); // Task 1
future2 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable2() {...}, 0, olddelay2, TimeUnit.SECONDS); // Task 2
future3 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable3() {...}, 0, olddelay3, TimeUnit.SECONDS); // Task 3
}
稍后在运行时我想更改其中一个线程的延迟(无论哪个线程),其他线程应使用旧延迟。我认为 ScheduledFuture 引用可能有所帮助并尝试了以下代码(例如第二个线程),但执行后线程池中只剩下一个线程(任务 2)。
public void changeDelay2(int newdelay2){
future2.cancel(true);
future2 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable2() {...}, 0, newdelay2, TimeUnit.SECONDS); // Task 2
}
那么有没有可能只改变一个线程的延迟?
您完全照原样做 - 您的代码中一定有其他错误。
这是一个完整的工作示例,它与您正在做的事情相同。下面的输出表明它正在按预期工作。
public class Reschedule {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
// Schedule three tasks
ScheduledFuture future1 = executor.scheduleAtFixedRate(() -> {
System.out.printf("%03ds: This is the first runnable, reporting in%n", (System.currentTimeMillis() - start) / 1000);
}, 0, 5, TimeUnit.SECONDS);
ScheduledFuture future2 = executor.scheduleAtFixedRate(() -> {
System.out.printf("%03ds: This is the second runnable, reporting in%n", (System.currentTimeMillis() - start) / 1000);
}, 2, 10, TimeUnit.SECONDS);
ScheduledFuture future3 = executor.scheduleAtFixedRate(() -> {
System.out.printf("%03ds: This is the third runnable, reporting in%n", (System.currentTimeMillis() - start) / 1000);
}, 5, 15, TimeUnit.SECONDS);
// Wait some
Thread.sleep(30000);
// Reschedule the second task
System.out.printf("%03ds: Rescheduling the second runnable to run at 20 second intervals%n", (System.currentTimeMillis() - start) / 1000);
future2.cancel(true);
future2 = executor.scheduleAtFixedRate(() -> {
System.out.printf("%03ds: This is the second runnable, reporting in%n", (System.currentTimeMillis() - start) / 1000);
}, 2, 20, TimeUnit.SECONDS);
}
}
正如您在下面的输出中看到的那样,重新安排第二个任务会重置它并将其从 运行 每 10 秒更改为每 20 秒,并且第一个和第三个任务不受影响。
输出:
000s: This is the first runnable, reporting in
002s: This is the second runnable, reporting in
005s: This is the first runnable, reporting in
005s: This is the third runnable, reporting in
010s: This is the first runnable, reporting in
012s: This is the second runnable, reporting in
015s: This is the first runnable, reporting in
020s: This is the first runnable, reporting in
020s: This is the third runnable, reporting in
022s: This is the second runnable, reporting in
025s: This is the first runnable, reporting in
030s: This is the first runnable, reporting in
030s: Rescheduling the second runnable to run at 20 second intervals
032s: This is the second runnable, reporting in
035s: This is the first runnable, reporting in
035s: This is the third runnable, reporting in
040s: This is the first runnable, reporting in
045s: This is the first runnable, reporting in
050s: This is the first runnable, reporting in
050s: This is the third runnable, reporting in
052s: This is the second runnable, reporting in
055s: This is the first runnable, reporting in
060s: This is the first runnable, reporting in
065s: This is the first runnable, reporting in
065s: This is the third runnable, reporting in
070s: This is the first runnable, reporting in
072s: This is the second runnable, reporting in
我向我的线程池添加了三个具有不同任务和不同延迟的线程,并且一切正常。
static ScheduledExecutorService scheduleTaskExecutor;
static ScheduledFuture<?> future1;
static ScheduledFuture<?> future2;
static ScheduledFuture<?> future3;
public void onCreate(Bundle savedInstanceState) {
scheduleTaskExecutor = Executors.newScheduledThreadPool(3);
future1 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable1() {...}, 0, olddelay1, TimeUnit.SECONDS); // Task 1
future2 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable2() {...}, 0, olddelay2, TimeUnit.SECONDS); // Task 2
future3 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable3() {...}, 0, olddelay3, TimeUnit.SECONDS); // Task 3
}
稍后在运行时我想更改其中一个线程的延迟(无论哪个线程),其他线程应使用旧延迟。我认为 ScheduledFuture 引用可能有所帮助并尝试了以下代码(例如第二个线程),但执行后线程池中只剩下一个线程(任务 2)。
public void changeDelay2(int newdelay2){
future2.cancel(true);
future2 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable2() {...}, 0, newdelay2, TimeUnit.SECONDS); // Task 2
}
那么有没有可能只改变一个线程的延迟?
您完全照原样做 - 您的代码中一定有其他错误。
这是一个完整的工作示例,它与您正在做的事情相同。下面的输出表明它正在按预期工作。
public class Reschedule {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
// Schedule three tasks
ScheduledFuture future1 = executor.scheduleAtFixedRate(() -> {
System.out.printf("%03ds: This is the first runnable, reporting in%n", (System.currentTimeMillis() - start) / 1000);
}, 0, 5, TimeUnit.SECONDS);
ScheduledFuture future2 = executor.scheduleAtFixedRate(() -> {
System.out.printf("%03ds: This is the second runnable, reporting in%n", (System.currentTimeMillis() - start) / 1000);
}, 2, 10, TimeUnit.SECONDS);
ScheduledFuture future3 = executor.scheduleAtFixedRate(() -> {
System.out.printf("%03ds: This is the third runnable, reporting in%n", (System.currentTimeMillis() - start) / 1000);
}, 5, 15, TimeUnit.SECONDS);
// Wait some
Thread.sleep(30000);
// Reschedule the second task
System.out.printf("%03ds: Rescheduling the second runnable to run at 20 second intervals%n", (System.currentTimeMillis() - start) / 1000);
future2.cancel(true);
future2 = executor.scheduleAtFixedRate(() -> {
System.out.printf("%03ds: This is the second runnable, reporting in%n", (System.currentTimeMillis() - start) / 1000);
}, 2, 20, TimeUnit.SECONDS);
}
}
正如您在下面的输出中看到的那样,重新安排第二个任务会重置它并将其从 运行 每 10 秒更改为每 20 秒,并且第一个和第三个任务不受影响。
输出:
000s: This is the first runnable, reporting in
002s: This is the second runnable, reporting in
005s: This is the first runnable, reporting in
005s: This is the third runnable, reporting in
010s: This is the first runnable, reporting in
012s: This is the second runnable, reporting in
015s: This is the first runnable, reporting in
020s: This is the first runnable, reporting in
020s: This is the third runnable, reporting in
022s: This is the second runnable, reporting in
025s: This is the first runnable, reporting in
030s: This is the first runnable, reporting in
030s: Rescheduling the second runnable to run at 20 second intervals
032s: This is the second runnable, reporting in
035s: This is the first runnable, reporting in
035s: This is the third runnable, reporting in
040s: This is the first runnable, reporting in
045s: This is the first runnable, reporting in
050s: This is the first runnable, reporting in
050s: This is the third runnable, reporting in
052s: This is the second runnable, reporting in
055s: This is the first runnable, reporting in
060s: This is the first runnable, reporting in
065s: This is the first runnable, reporting in
065s: This is the third runnable, reporting in
070s: This is the first runnable, reporting in
072s: This is the second runnable, reporting in