0

I am sending a request to some servers in parallel but their responses don't come at the same time. I have to show the response on an html page. One way is to wait for all the responses and when all the responses arrive, show them. What I want to do is to show a response as soon as it comes and don't wait for other responses. A rough algorithm looks like:

while(all the responses don't come or timeout occurs)
    waitForResponse();
    if(responseArrived==true){
         //put it on html page
    }
}

How can I do it in java?

Vivek Mangal
  • 532
  • 1
  • 8
  • 24

1 Answers1

0

Sounds like you are looking for Futures. They allow you to create a parallel task which will take all the time it needs to be executed, and have method get, which will return null if task is not yet done, so you can write something like

while(!future.isDone()){
  Thread.sleep(1000);
}

And after exiting cycle get your value with future.get();

There are more to it, so please see through this article, it is small but exactly fits what you are asking for

Dmytro Grynets
  • 923
  • 11
  • 29
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – OneCricketeer Feb 15 '17 at 18:33