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

  • SEARCH
  • Home
  • 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 7619661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:44:36+00:00 2026-05-31T03:44:36+00:00

I use a scheduler to ensure that my loop does not take too long,

  • 0

I use a scheduler to ensure that my loop does not take too long, so I exit the loop if the scheduler fires and ends the loop.

This is quite straight forward:

public void startSchedulerForTimeout() {

    Log.i("x", "SCHEDULING TIMEOUT " );
    timeoutForLoopReached = false;
    scheduler.schedule(new Runnable() {
    public void run() {
        timeoutForLoopReached = true;
        Log.i("x", "SET TIMEOUT FLAG ");
        }
     }, 10, TimeUnit.SECONDS);
}

so in my code I just write:

startSchedulerForUSBMessageTimeout();
while (!done && !timeoutForLoopReached) {
  ...do the loop stuff
}

To make sure that the scheduled task does not fire when I run the loop again I need to unschedule the obsolete task (in case I ended the loop before timeout) so that the new timer will start again at the beginning of a new loop (instead of the old one firing after the first loop has been finished right in between the second time the loop executes)

How can I just unschedule any scheduled task that has not been fired to avoid this scheduled task mixup?

Many thanks!

  • 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-31T03:44:37+00:00Added an answer on May 31, 2026 at 3:44 am

    Use ScheduledFuture:

    ScheduledExecutorService scheduledTaskExecutor = Executors.newScheduledThreadPool(1);
    Runnable longRunningTask = new Runnable();
    
    // schedule long running task in 2 minutes:
    ScheduledFuture scheduleFuture = scheduledTaskExecutor.schedule(longRunningTask, 2, TimeUnit.MINUTES);
    
    ... ...
    // At some point in the future, if you want to cancel scheduled task:
    scheduleFuture.cancel(true);
    ... ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why not use the Windows scheduler? I have several applications that have to run
Does anyone know of a Calendar scheduler control that would display a whole month
Our use of Quartz so far has been to configure the database backed scheduler
I am looking to use quartz to schedule emails, but I'm not sure which
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move
Use case: A does something on his box and gots stuck. He asks B
I'm developing add-on based application. Every add-on use scheduler. Loaded add-on schedule task. My
I am into shared hosting and they do not allow me to use windows
I want to use Windows Task Scheduler to execute my ASP.NET web page (.aspx).
Please tell me how can i use/integrate/get Outlook's Scheduler in my asp.net application. i

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.