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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:41:06+00:00 2026-06-16T07:41:06+00:00

I have a ScheduledExecutorService , which invokes a Runnable periodically via scheduleWithFixedDelay() (could have

  • 0

I have a ScheduledExecutorService, which invokes a Runnable periodically via scheduleWithFixedDelay() (could have used scheduleAtFixedRate() instead).

Am now considering what to do if an error occurs. If it’s something that can’t easily be recovered from(*) I’d like the option of stopping all further invocations but not sure of the best way of doing this.

Apparently checked exceptions can’t be thrown from a Runnable so would appreciate any guidance on how to choose from the following:

scheduledFuture.cancel(false);
...or...
scheduledFuture.cancel(true);
...or...
scheduledExecutorService.shutdown();
...or...
scheduledExecutorService.shutdownNow();
...or...
Throw a custom RuntimeException myself?
...or...
Something else?

(*) Would like to know the general case but in case anyone’s interested, the checked exception I’m currently looking at is a ParserConfigurationException thrown from DocumentBuilderFactory.newDocumentBuilder(). If this is thrown, it indicates a serious problem so I’d basically like the scheduling to completely stop rather than potentially repeating the error every time.

  • 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-16T07:41:07+00:00Added an answer on June 16, 2026 at 7:41 am

    Based on a few of the helpful comments above here’s the gist of my current code – a few q’s remain within so would welcome any further comments:

    public class ScheduledTask implements Runnable {
        // Configurable values
        private static final int CORE_THREAD_POOL_SIZE = 1;
        private static final int INITIAL_DELAY_MS = 0;
        private static final int INTERVAL_MS = 1000;
    
        private final ScheduledExecutorService scheduledExecutorService = 
            Executors.newScheduledThreadPool(ScheduledTask.CORE_THREAD_POOL_SIZE);
    
        private ScheduledFuture<?> scheduledFuture;
    
        public void run() {
            try {
                try {
                    // Do stuff
                } catch RecoverableCheckedException rce { // E.g. SAXException
                    // Log and handle appropriately
                }
            } catch UnrecoverableCheckedException uce { // E.g. ParserConfigurationException
                // Not 100% happy with this. It means the caller would need to call
                // getCause() to get the real Exception in this case. But other 
                // RuntimeExceptions wouldn't be wrapped. Could consider catching
                // and wrapping all RuntimeExceptions but I like that even less!
                throw new RuntimeException(uce);
            }
        }
    
        public boolean isScheduling() {
            return (this.scheduledFuture != null)
                   && (!this.scheduledFuture.isDone());
        }
    
        // May not be needed but provided in case this class is shared.
        public boolean isShutdown() {
            return scheduledExecutorService.isShutdown();
        }
    
        public void start() {
            // If the Executor Service has already been shutdown, would expect
            // a RejectedExecutionException to be thrown here(?) Not sure what
            // would happen if this method were called when isScheduling() is
            // true?
            this.scheduledFuture = 
                this.scheduledExecutorService.scheduleWithFixedDelay(
                    this,
                    ScheduledTask.INITIAL_DELAY_MS,
                    ScheduledTask.INTERVAL_MS,
                    TimeUnit.MILLISECONDS);
        }
    
        // To be called once at the very end - e.g. on program termination.
        public void shutdown() {
            this.scheduledExecutorService.shutdown();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ScheduledExecutorService that times a few different task periodically with scheduleAtFixedRate(Runnable, INIT_DELAY,
i have a ScheduledExecutorService that gets tasks for periodically execution: scheduler = Executors.newScheduledThreadPool( what
I have a poller implemented from Runnable. The poller passes itself into a ScheduledExecutorService.
I have a question regarding the scheduleAtFixedRate() method on ScheduledExecutorService in Java 6. [
I have a ScheduledExecutorService and I do a task every 15 mins (in a
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Have deployed numerous report parts which reference the same view however one of them
I have a very complex system (100+ threads) which need to send email without
I have an object that needs to do periodically do some work while the

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.