This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Handling multiple requests using CompletableFuture

 
Ranch Hand
Posts: 586
6
jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have a multi threading problem where there are bunch of requests which will take varied time for processing. I want to  get response of all the requests and consume the responses.

My plan is to use CompletableFuture but not able to come up with how to code it. I created a mock up using ExecutorService and here too I am not sure how to handle the future result.
Would it be better to wait for a response with blocking call such as get() or should I use a while loop and loop around until all results are either done or canceled?

Here is the current mock up :
TimeUtil.java


DummyRequest.java


RequestHandler.java


VariableTimedRequestProcessing.java


Would like to get advice on:
1) handling future result in current case.
2) if I use CompletableFuture, what should be the approach to this problem ?

Thanks
 
Marshal
Posts: 24950
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You could do that. I'm not sure what the difference would be if you used CompletableFuture instead but perhaps you could explain what you thought the difference would be when you planned to use it.
 
s ravi chandran
Ranch Hand
Posts: 586
6
jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

You could do that. I'm not sure what the difference would be if you used CompletableFuture instead but perhaps you could explain what you thought the difference would be when you planned to use it.


Thanks for the response. I updated my function with what you provided, hope this is how we should be handling the future results.


Coming back to by intention of using CompletableFuture, well I wanted to create sort of workflow pipeline and handle the result from all requests asynchronously.
But as I need the list of responses to process those further, that part is little murky for now.

Here is an incomplete code which I am trying to work out.
 
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!