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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:31:09+00:00 2026-06-14T16:31:09+00:00

I am working on a service that returns a List<Response> object. There are two

  • 0

I am working on a service that returns a List<Response> object.

There are two kind of object that can build the response. One of them populates the content from internal data, the other reaches out to a service and gets the data.

for(Item item : items){
   if(item is external){
     call service and get result and populate Response and add to List
   }

   if(item is internal)
   {
     populate response object and add to list
   }
}

Current impl is procedural and blocking, what i want is a non blocking design such that, if the item is external fire the call and continue with next item in the list. Then when the loop is done, i can wait for all to finish.

What would be a good approach to do this? I m also considering creating seperate classes per responsibility.

EDIT: the reason to introduce is to reduce latency hits.

  • 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-14T16:31:10+00:00Added an answer on June 14, 2026 at 4:31 pm

    ExecutorCompletionService (thread pool with callbacks that remembers submitted tasks) seems like a goof git:

    CompletionService<Response> executorService = 
        new ExecutorCompletionService<>(Executors.newFixedThreadPool(10));
    int totalExternal = 0;
    
    for(Item item : items){
        if(item is external){
            executorService.submit(externalCall(item));
            ++totalExternal;
        }
    
        if(item is internal){
            populate response object and add to list
        }
    }
    
    for (int i = 0; i < totalExternal; ++totalExternal) {
        addAsynchResultToResponseList(executorService.take().get());
    }
    

    Where externalCall(item) is defined as follows for clarity:

    Callable<Response> externalCall(Item item) {
      return new Callable<Response>() {
        //...
      }
    }
    

    Obviously once you go asynchronous the result list can have an arbitrary order.


    Another approach would be to use ordinary ExecutorService and have an intermediate List<Future<Response>>. The trick is to use AsyncResult wrapper to wrap internal responses (it creates Future that is immediately done and returns passed value).

    List<Future<Response>> futures = new ArrayList<>();
    for(Item item : items){
        if(item is external){
            futures.add(executorService.submit(externalCall(item)));
        }
    
        if(item is internal){
            futures.add(new AsyncResult(synchResponse));
        }
    }
    

    Now you can simply iterate over futures. AsyncResult will return immediately as the value was already computer when it was created (synchResponse). But you will have to wait for Futures returned from a thread pool.

    Remember that Future.get() allows you to retrieve original exception. Also the order of Futures in the list is the same as the order of original items, so if nth Future fails, nth item in items list was the cause.

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

Sidebar

Related Questions

I have a service layer that exposes a method, which returns me a List,
I am working on a RESTful web service, that will return a list of
I'm currently working with web services that return objects such as a list of
I have modified a working Windows service that had always been starting beforehand. After
I'm working on creating a windows service that will send emails to a customer
I'm working on a WCF service that requires Membership and Profile access. I got
I'm working with a web service that will give me values like: var text
I'm working on a Windows Service that's having some issues with Thread.Sleep() so I
I'm currently working on a WCF service that reaches out to another service to
I'm working on a Rails-based web service that provides data about various sports team

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.