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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:26:37+00:00 2026-06-10T11:26:37+00:00

I have the following example code and let’s say that MyCallable(B) takes longer than

  • 0

I have the following example code and let’s say that MyCallable("B") takes longer than one second to execute, when the others execute quicker than one second. Therefore inside my loop that calls Future.get(), it will throw a TimeoutException.

public static void main(String[] args) {
    ExecutorService es = Executors.newFixedThreadPool(2);

    List<Future<String>> futures = new ArrayList<Future<String>>();

    futures.add(es.submit(new MyCallable("A")));
    futures.add(es.submit(new MyCallable("B")));
    futures.add(es.submit(new MyCallable("C")));
    futures.add(es.submit(new MyCallable("D")));
    futures.add(es.submit(new MyCallable("E")));

    try {
        for(Future<String> f  : futures) {
            try {
                System.out.println("result " + f.get(1, TimeUnit.SECONDS));
            }
            catch (TimeoutException e) {
                // how do I know which MyCallable() has timed out?
            } catch (ExecutionException e) {
                System.out.println(e.getMessage());
            }
        }
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
    finally {
        es.shutdown();
    }
}

As expected each of the MyCallable() instances execute, but for the one that times out I would like to perform some error handling and this requires knowing which Callable is associated with which Future.

Is there a mechanism for this association or is it up to my Callable to handle all the error processing inside it’s call() method?

  • 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-10T11:26:38+00:00Added an answer on June 10, 2026 at 11:26 am

    Seems like you could simply maintain a Map<Future<String>, Callable<String>> instead of a List<Future<String>> and retrieve the original Callable that way.

    If you want to get really clever, you could do it OO-style and extend ThreadPoolExecutor and create a Future decorator class. I think this is probably overkill, but you could do it like this:

    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.Future;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.TimeoutException;
    
    
    public class FutureWithCallable<T> implements Future<T> {
        private final Callable<T> callable;
        private final Future<T> wrapped;
    
        public FutureWithCallable(Future<T> wrapped, Callable<T> callable) {
            this.callable = callable;
            this.wrapped = wrapped;
        }
    
        public Callable<T> getCallable() {
            return callable;
        }
    
        @Override
        public boolean cancel(boolean mayInterruptIfRunning) {
            return wrapped.cancel(mayInterruptIfRunning);
        }
    
        @Override
        public T get() throws InterruptedException, ExecutionException {
            return wrapped.get();
        }
    
        @Override
        public T get(long timeout, TimeUnit unit) throws InterruptedException,
                ExecutionException, TimeoutException {
            return wrapped.get(timeout, unit);
        }
    
        @Override
        public boolean isCancelled() {
            return wrapped.isCancelled();
        }
    
        @Override
        public boolean isDone() {
            return wrapped.isDone();
        }
    }
    

    And then:

    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.Callable;
    import java.util.concurrent.Future;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    
        public class ExecutorServiceWithCallable extends ThreadPoolExecutor {
    
            public ExecutorServiceWithCallable(int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
                super(corePoolSize, maxPoolSize, keepAliveTime, unit, workQueue);
            }
    
            @Override
            public <T> FutureWithCallable submit(Callable<T> callable) {
                Future<T> future = super.submit(callable);
                return new FutureWithCallable<T>(future, callable);
            }
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say suppose I have the following Java code. public class Example { public static
Say I have the following code: <div onclick='location.href=http://www.example.com/'> <a href='#' onclick='alert(blah)'>click</a> </div> Is there
Let's say I have the following code: // templateClass.h #ifndef TEMPLATE_CLASS_H #define TEMPLATE_CLASS_H template
I will explain my question on an example. Let's have following code in C#:
Let's say I have the following code... StringBuilder sb = new StringBuilder(); for (int
I have the following example code: $dataProvider = new CActiveDataProvider('firstTable', array('criteria' => array( 'select'
On a report I have the following code for a field: =Sum([PartQty]*[ModuleQty]) Example results
Let me give you the following example: Imagine you have a window class which
Suppose I have following code: def foo(s): A dummy function foo. For example: >>>
let's say I have the following dictionary : public Dictionary<Room, List<Booking>> rooms = new

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.