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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:57:34+00:00 2026-05-30T23:57:34+00:00

I use the ExecutorService in Java and I noticed a behaviour I dont understand.

  • 0

I use the ExecutorService in Java and I noticed a behaviour I dont understand.
I use Callable and when I invoke my threads (classes that implement Callable) I set a timeout. Then I wait for the result with future.get() and after I wanted to check with future.isDone() if a timeout occurs during executing the tasks.

As I read in the documentation for invokeAll with timeout: returns a list of Futures representing the tasks, in the same sequential order as produced by the iterator for the given task list. If the operation did not time out, each task will have completed. If it did time out, some of these tasks will not have completed.

So I thought I would get a list of Future results in both cases, if a timeout occurs and if it does not.

What now happens is the following: When a timeout occurs, the code does not go on after future.get() and I dont ever get to the point where I could check if a timeout occurs with future.isDone(). I dont catch any exception, Im directly leaded to the finally block in my code, which I really dont understand.

here is a snippet of my code:

     try {
        // start all Threads
        results = pool.invokeAll(threads, 3, TimeUnit.SECONDS);

        for (Future<String> future : results)
        {
            try
            {
                // this method blocks until it receives the result, unless there is a 
                // timeout set.
                final String rs = future.get();

                if (future.isDone())
                {
                    // if future.isDone() = true, a timeout did not occur. 
                   // do something
                }
                else
                {
                    // timeout
                    // log it and do something
                    break;
                }
            }
            catch (ExecutionException e)
            {
               // log messages and break, this is a snippet!
            }
            catch (InterruptedException ex)
            {
               // log message and break, this is a snippet!
            }
        }

    }
    catch (InterruptedException ex)
    {
        // log message, this is a snippet!
    }
    finally
    {
        // when a timeout occurs, the code jumps from future.get() directly to this point!
    }

Might somebody explain me, why I am not able to reach future.isDone() and what I should change to be able to recognize timeouts?

Thank you!

  • 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-30T23:57:36+00:00Added an answer on May 30, 2026 at 11:57 pm

    You are not catching CancellationException, which is very likely being thrown after calling get. Note that this exception extends RuntimeException and the compiler won’t warn you to catch it.

    Reading the docs for invokeAll:

    Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first. Future.isDone() is true for each element of the returned list. Upon return, tasks that have not completed are cancelled.

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

Sidebar

Related Questions

I am confused on the following: To use threads in a Java program, the
Since use ExecutorService can submit a Callable task and return a Future , why
Possible Duplicate: Handling exceptions from Java ExecutorService tasks I use the ExecutorService from Java
We use a data acquisition card to take readings from a device that increases
I am trying to use futures for the first time. It seems smart that
I am executing a Callable Object using ExecutorService thread pool. I want to give
I currently use FixedThreadPool to download images from the web, like this: ExecutorService mThreadPool
my service layer methods are transactional, when i use ExecutorService and submit task to
INITIAL QUESTION: I have an app that uses ExecutorService to run 4 fixedThreadPool. When
I've been reading Filthy Rich Clients lately and noticed that, although the version of

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.