Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9119729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:27:54+00:00 2026-06-17T05:27:54+00:00

I have a pool of threads dedicated to execute asynchronous tasks, using a shared

  • 0

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 :

  1. At time t=0, submit a first task to execute at t=30
  2. At t=10, submit a second task to execute at t=20
  3. At t=15, postpone the second task from t=20 to t=100
  4. t=30 arrive, but the first task isn’t executed, it stays in the queue. Its getDelay method now starts returning negative values.
  5. 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.
  6. 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.
  7. 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 :

  1. Why it is so ? Am I doing something wrong in my code ?
  2. 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 ?
  3. 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> {
...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:27:55+00:00Added an answer on June 17, 2026 at 5:27 am

    A question remains though: is it possible to reschedule the object that was just cancelled

    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.

    At time t=0, submit a first task to execute at t=30

    schedule a task for 30 later.

    At t=10, submit a second task to execute at t=20

    schedule a task for 10 later and save the Future.

    At t=15, postpone the second task from t=20 to t=100

    Future.cancel and reschedule

    t=30 arrive, but the first task isn’t executed, it stays in the queue. Its getDelay method now starts returning negative values.

    In this example it would execute unless you had cancelled it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pool of threads which are fed tasks from a queue. Generally,
I have a thread pool with threads doing specific tasks. After the task is
I have a custom thread pool class, that creates some threads that each wait
I have a pool of threads which are working independently. Basically these threads are
i have thread pool that create threads each thread worker calculate some work and
I want to write a server using a pool of worker threads and an
I have developed an object pool and cannot seem to do it without using
I'm trying to develop an application that consists of a pool of threads, using
I have a thread pool created using java.util.concurrent.ThreadPoolExecutor Is there anyway I can wait
I have a class that I've created to allow asynchronous sequential execution of tasks,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.