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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:04:31+00:00 2026-05-10T23:04:31+00:00

I came up with a solution, but I wanted to see if anyone had

  • 0

I came up with a solution, but I wanted to see if anyone had a better way or more standard way of doing this.

The main method demonstrates how I would use this class. In a production version of this I would keep the thread pool open and continue to reuse it rather than shut it down after the calls.

public class MultiCallManager<T> {  private ExecutorService service; private Map<String, Future<T>> futureResults; private Map<String, T> results; private Map<String, Exception> errors;  public MultiCallManager(ExecutorService inService) {     service = inService;     futureResults = new LinkedHashMap<String, Future<T>>();     results = new LinkedHashMap<String, T>();     errors = new LinkedHashMap<String, Exception>(); }  public void add(String key, Callable<T> task) {     Future<T> result = service.submit(task);     futureResults.put(key, result); }  public boolean hasErrors() {     return !errors.isEmpty(); }  public T getResult(String key) {     return results.get(key); }  public Exception getException(String key) {     return errors.get(key); }  public void waitUntilDone(long timeout) {     for (Entry<String, Future<T>> entry : futureResults.entrySet())     {         if (timeout < 0)         {             timeout = 0;         }         long start = System.currentTimeMillis();         try         {             results.put(entry.getKey(), entry.getValue().get(timeout, TimeUnit.MILLISECONDS));         }         catch (Exception e)         {             errors.put(entry.getKey(), e);         }         long end = System.currentTimeMillis();         long time = end - start;         timeout = timeout - time;     } }  public static void main(String[] args) throws Exception {     ExecutorService service = Executors.newFixedThreadPool(5);      MultiCallManager sandbox = new MultiCallManager(service);      final String value = 'AWESOME!';      Callable<Object> c1 = new Callable<Object>()     {          @Override         public String call() throws Exception         {             Thread.sleep(5000);             return value;         }     };      Callable<Object> c2 = new Callable<Object>()     {          @Override         public Object call() throws Exception         {             Thread.sleep(6000);             return value;         }     };      Callable<Object> c3 = new Callable<Object>()     {          @Override         public Object call() throws Exception         {             Thread.sleep(2000);             return value;         }     };      sandbox.add('c1', c1);     sandbox.add('c2', c2);     sandbox.add('c3', c3);     sandbox.waitUntilDone(5000);      if (sandbox.getResult('c1') != null)     {         System.out.println(sandbox.getResult('c1'));     }     else     {         sandbox.getException('c1').printStackTrace();     }     if (sandbox.getResult('c2') != null)     {         System.out.println(sandbox.getResult('c2'));     }     else     {         sandbox.getException('c2').printStackTrace();     }     if (sandbox.getResult('c3') != null)     {         System.out.println(sandbox.getResult('c3'));     }     else     {         sandbox.getException('c3').printStackTrace();     }     service.shutdownNow(); } 

}

  • 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. 2026-05-10T23:04:31+00:00Added an answer on May 10, 2026 at 11:04 pm

    Have a look at CompletionService and/or CountDownLatch they would accomplish some of what you are doing with a little nicer code.

    • CountdownLatch example
    • Article with CompletionService examples

    CompletionService provides an interface similar to a BlockingQueue, so you can use the poll(long timeout, TimeUnit unit) method. Each time you poll, you’d want to set the timeout to the amount of time remaining for all threads, and just exit when time is up.

    This essentially replaces your waitUntilDone method, performing the same job.

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

Sidebar

Related Questions

I came across this thread when I was looking for a solution, but it
I recently came across this problem and found a solution but I'm wondering if
I came with below solution but I believe that must be nicer one out
This was originally a question I wanted to ask, but while researching the details
I know that attributes are not supported inside the method body but i wanted
I've been dealing with this before but it seems like the problem came back.
I have came with solution to remove duplicates from generic list<T> in .NET 2.0
So basically I came up with a semi home grown solution to lazy loading
Came across this code: <?php require_once 'HTTP/Session/Container/DB.php'; $s = new HTTP_Session_Container_DB('mysql://user:password@localhost/db'); ini_get('session.auto_start') or session_start();
(Came up with this question in the course of trying to answer this other

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.