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 356811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:08:48+00:00 2026-05-12T12:08:48+00:00

I have the following problem that the standard library doesn’t solve well, and I’m

  • 0

I have the following problem that the standard library doesn’t solve well, and I’m wondering if anybody has seen another library out there than can do it so I don’t need to hack together a custom solution. I have a task that is currently scheduled on a thread pool using scheduleWithFixedDelay(), and I need to modify the code to handle requests for “urgent” execution of the task related to asynchronous events. Thus, if the task is scheduled to occur with a delay of 5 minutes between executions, and an event occurs 2 minutes after the last completed execution, I would like to execute the task immediately and then have it wait for 5 minutes after the completion of the urgent execution before it runs again. Right now the best solution that I can come up with is to have the event handler call cancel() on the ScheduledFuture object returned by scheduleWithFixedDelay() and execute the task immediately, and then set a flag in the task to tell it to reschedule itself with the same delay parameters. Is this functionality available already and I’m just missing something in the documentation?

  • 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-05-12T12:08:49+00:00Added an answer on May 12, 2026 at 12:08 pm

    If you are using ScheduledThreadPoolExecutor there is a method decorateTask (well in fact there are two, for Runnable and Callable tasks) that you can override to store a reference to the task somewhere.

    When you need urgent execution, you just call run() on that reference which makes it run and rescheduled with same delay.

    A quick hack-up attempt:

    public class UrgentScheduledThreadPoolExecutor extends
            ScheduledThreadPoolExecutor {
        RunnableScheduledFuture scheduledTask;
    
        public UrgentScheduledThreadPoolExecutor(int corePoolSize) {
            super(corePoolSize);
        }
    
        @Override
        protected  RunnableScheduledFuture decorateTask(Runnable runnable,
                RunnableScheduledFuture task) {
            scheduledTask = task;
            return super.decorateTask(runnable, task);
        }
    
        public void runUrgently() {
            this.scheduledTask.run();
        }
    }

    which can be used like this:

    public class UrgentExecutionTest {
    
        public static void main(String[] args) throws Exception {
            UrgentScheduledThreadPoolExecutor pool = new UrgentScheduledThreadPoolExecutor(5);
    
            pool.scheduleWithFixedDelay(new Runnable() {
                SimpleDateFormat format = new SimpleDateFormat("ss"); 
    
                @Override
                public void run() {
                    System.out.println(format.format(new Date()));
                }
            }, 0, 2L, TimeUnit.SECONDS);
            Thread.sleep(7000);
            pool.runUrgently();
            pool.awaitTermination(600, TimeUnit.SECONDS);
        }
    }

    and produces the following output:
    06
    08
    10
    11
    13
    15

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

Sidebar

Related Questions

I have the following query of linq to entities. The problem is that it
I have got the following problem since the server has safe mode turned on,
Has anyone encountered the following problem: I have IIS7 running on my computer. On
I have the following problem: I have an HTML textbox ( <input type=text> )
I have the following problem using subversion: I'm currently working on the trunk of
I have the following problem using template instantiation [*]. file foo.h class Foo {
I have the following problem in my Data Structures and Problem Solving using Java
I have the following problem: I open the dialog, open the SIP keyboard to
I have the following problem. If I query values with a keyfigure which is
Avast there fellow programmers! I have the following problem: I have two rectangles overlapping

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.