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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:39:45+00:00 2026-05-19T04:39:45+00:00

I’m implementing some code using the java.util.concurrency framework. I will be passing a collection

  • 0

I’m implementing some code using the java.util.concurrency framework. I will be passing a collection of callables to a class which will execute them in parallel. I’m trying to work out the best way of getting a hold of each response in a type safe manner. Here’s some code to help explain what I’m doing:

First I create my Callables, which are units of work to be called in parallel. For example, here the first unit of work returns a String and the second an Integer:

Callable<String> firstCallable = new Callable<String>(){
   public String call() {...}
};

Callable<Integer> secondCallable = new Callable<Integer>(){
   public Integer call() {...}
};

Now I fire them into my framework to run them in parallel and the trick is getting a handle to the appropiate response Object. Here’s an implementation that works:

Map<Callable,Object> responseMap = ParallelSender.send(firstCallable, 
                                                       secondCallable);

where Object is the response of a particular Callable. Thus, you can do this:

String firstCallableResponse = (String)responseMap.get(firstCallable);
Integer secondCallableResponse = (Integer)responseMap.get(secondCallable);

So my question is, is it possible to avoid casting when fetching from the Map? This doesn’t compile, but I’m thinking along these lines:

Map<Callable<T>, T> responseMap = ParallelSender.send(...);
String firstCallableResponse = responseMap.get(firstCallable);

such that the value returned is based on the typed parameter of the Callable key. My concern is that if anyone refactors the return type of the unit of work (say from Integer to BigDecimal or whatever) then the cast from Object will never be caught by automatic refactor tools and could lead to runtime issues.


CONCLUSION: Thanks to all the helpful comments and discussion below, I have taken a slightly different tact (though credited Sean Patrick Floyd with a correct to my question above). I ended up dropping the response map alltogether and populating the Callable object with the response. Here are the relevant code snippets:

public abstract class AbstractParallelCallable<V> implements Callable<V> {

   /** The response generated by the Call method of this class. */
   private V callableResponse;

   public V getResponse() {
       return callableResponse;
   }

   public void setResponse(V response) {
       callableResponse = response;
   }
}

Thus, I have an abstract implementation which wrappers the Callable object by storing the response. Next,in my parallel processing I get the response each Future created and populate the AbstractParallelCallable:

for (ParallelFutureTask<Object> future : futures) {
   try {
      future.getCallableHandle().setResponse(future.get());
   } catch(Exception e) {...}
}

where getCallableHandle returns the AbstractParallelCallable object and ParallelFutureTask wrappers FutureTask by providing a reference to the Callable object. After execution, the calling code can then do this:

Integer theResult = firstCallable.getResponse();
  • 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-19T04:39:45+00:00Added an answer on May 19, 2026 at 4:39 am

    Method-based approach

    The only way you can do it is to encapsulate it in a method:

    class ParallelSender{
    
        private final Map<Callable<?>, Object> map =
            new HashMap<Callable<?>, Object>();
    
        @SuppressWarnings("unchecked")
        public <T> T getResult(final Callable<T> callable){
            return (T) map.get(callable);
        }
    
    }
    

    Client code

    Now your client code does not need to cast:

    ParallelSender parallelSender = new ParallelSender();
    Callable<Integer> integerCallable = new Callable<Integer>(){
    
        @Override
        public Integer call() throws Exception{
            return Integer.valueOf(1);
        }
    };
    Integer result = parallelSender.getResult(integerCallable);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
Seemingly simple, but I cannot find anything relevant on the web. What is 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.