I add three threads with different tasks and different delays to my threadpool and everything works pretty fine.
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
}
Later during runtime i want to change the delay of one of these threads (no matter which), the others shall work on with the old delay. I thought the ScheduledFuture references may help and tried the following code (for example for the second thread), but after execution there is only one thread left in the threadpool (Task 2).
public void changeDelay2(int newdelay2){
future2.cancel(true);
future2 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable2() {...}, 0, newdelay2, TimeUnit.SECONDS); // Task 2
}
So is there a possibility to change the delay of only one thread?
You do it exactly as you have done - there must be some other error in your code.
Here's a complete, working example that does the same thing you're doing. The output below shows that it is working as intended.
As you can see in the below output rescheduling the second task resets it and changes it from running every 10 seconds to every 20 seconds and the first and third tasks are unaffected.
Output: