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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:03:45+00:00 2026-06-12T14:03:45+00:00

I am trying to run the following piece of code: public static void main(String[]

  • 0

I am trying to run the following piece of code:

public static void main(String[] args){
    ScheduledExecutorService service = new ScheduledThreadPoolExecutor(2);

    Runnable r = new Runnable() {
        @Override
        public void run() {
            throw new RuntimeException();
        }
    };
    service.execute(r );
    ScheduledFuture<?> schedule = service.schedule(r, 0, TimeUnit.SECONDS);
    new Thread(r).run();
}

Regarding the above I have the following questions:

  1. Is there any way to catch and respond to exceptions happening on the executor’s thread?
  2. Why is the exception from the thread created explicitly propagated to the main thread, but both executions using the executor service does not propagate that error? How can this error ever be discovered?

EDIT: One further question came to mind:

  1. How can i stop a given periodic task that I schedule, let’s say after N repeats or N minutes?
  • 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-12T14:03:46+00:00Added an answer on June 12, 2026 at 2:03 pm

    Question 2 is really easy – you’re not actually starting a new thread, you’re just calling run(), which runs synchronously in the original thread. You should be calling start(), at which point the exception won’t be propagated back.

    As for handling exceptions in a ScheduledExecutorService – if you call Future.get(), it will throw ExecutionException if the original task threw an exception, exposing the original exception as the cause:

    Exception thrown when attempting to retrieve the result of a task that aborted by throwing an exception. This exception can be inspected using the Throwable.getCause() method.

    If you need to respond to exceptions without blocking for the future to complete, you could wrap your “real” Runnable in another one which just delegated to the original’s run() method, but with an appropriate try/catch block.

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

Sidebar

Related Questions

I am new to python and I am trying to run following code: m=int(raw_input())
when I trying to run following code. var result = from c in db.brand
I am trying to run the following code that I found marked as correct
I am trying to run the following code from within Eclipse: Process process =
I am trying to run following code: $a = array('aa'); function my_func (& $m)
I am trying to run the following code to loop around a recordset and
Here is the following piece of code that I am trying to execute on
I am trying to run following code: with open('file.sql') as sqlFile: sql = sqlFile.read()
Consider the following piece of Haskell code: type Parser a = String -> [(a,
I'm trying to run this piece of code inside my onCreate method as an

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.