I have a pool of threads dedicated to execute asynchronous tasks, using a shared DelayQueue.
Basicly, everything works well, except one thing: I would like to be able to postpone the execution of some already scheduled task.
For example, let’s say that I submit now at time t=0 a task to execute in 30 seconds. After 10 seconds (t=10), I decide that, oh no, that task wont be executed at t=30 but at t=50; I have thus postponed it to 20 seconds later.
For that purpose, I have the postpone method, which modify the time set for the task and thus change the returned value of getDelay. The code is at the end of this post.
Infortunately, it doesn’t work. It is actually very easy to break the system and do so that expired elements remain in the queue, much longer that they normally should.
More specifically, I observed the following unwanted behavior :
- At time t=0, submit a first task to execute at t=30
- At t=10, submit a second task to execute at t=20
- At t=15, postpone the second task from t=20 to t=100
- t=30 arrive, but the first task isn’t executed, it stays in the queue. Its getDelay method now starts returning negative values.
- t=40: the first task is already 10 seconds late, and still nothing happens. getDelay on the first task returns more and more smaller values as the time goes; the DelayQueue seems to be definitely mixed up.
- t=90: perhaps an hope, because of the 30 seconds maximum poll time set in the q.poll call. In fact, no, I get a null and proceed to wait for the next task; my first task still stays in the queue with a negative delay.
- t=100: hourra ! both tasks are executed right one after the other… the second one is on time, but the first one arrived finally 70 seconds late. This is unacceptable !
I also noticed that, if the task has never been the head while it was into the queue, I was able to postpone it safely, i.e. without disturbing other tasks.
So, my questions are :
- Why it is so ? Am I doing something wrong in my code ?
- Do I necessarily have to remove the task, then submit it again, to simulate a postpone ? Or is there another way to do it safely ? Am I really allowed to remove an object and then re-add that exact same object, or would it be preferable to submit another one to be sure to avoid all possible confusion ?
- Bonus question: what is the complexity of the remove operation ? Presumabely O(n), if I assume that the queue is implemented as a kind of priority heap (it is impossible to make a binary search in a priority heap).
Thank you for your answers.
Here is the code. I have removed as much irrelevant parts as I can. I have especially deleted all exception handling.
public abstract class AbstractTaskExecutor<R extends Runnable> implements Runnable {
private final BlockingQueue<R> q;
...
public boolean submit (R dr) { return q.add(dr); }
public void run () {
while (!Thread.currentThread().isInterrupted()) {
Runnable r = q.poll(30, TimeUnit.SECONDS);
if (r!=null) r.run();
}}
}
public abstract class DelayedRunnable implements Runnable, Delayed {
private long time;
public DelayedRunnable (long l) {
time = System.currentTimeMillis() +l;
}
public final int compareTo (Delayed d) {
return (int)( Math.min(Math.max(Integer.MIN_VALUE, time - ((DelayedRunnable)d).time), Integer.MAX_VALUE) );
}
public final long getDelay (TimeUnit t) {
return t.convert(time - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}
public final long getTime () { return time; }
public void postpone (long l) { time+=l; }
}
public class DelayedTaskExecutor extends AbstractTaskExecutor<DelayedRunnable> {
...
}
Not if all you are doing is changing the time of “activation” You have to remove it, change it and add it back in as it is likely to be placed differently in the data structure. This is required as the structure determines the order of events and if you just change the value this may or may not lead to the order changing. You get similar problems if you change a key of Map after you add it. i.e. the structure does not behave correctly.
I would use a ScheduledExecutorService to wrap the delayed queue and thread pool.
When you place a delayed task you get back a Future object which you can cancel and reschedule as required.
schedule a task for 30 later.
schedule a task for 10 later and save the Future.
Future.cancel and reschedule
In this example it would execute unless you had cancelled it.