Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using CompletableFuture to to notify task completion

 
s ravi chandran
Ranch Hand
Posts: 452
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a requirement related to a list of runnable tasks. What I want is that when I execute a list of runnables, I want to know when each one of them completes and then trigger another action based on that.

Here is the initial code:






From Javadoc I see that I can use CompletableFuture for my purpose. I think runAsync() method looks useful. But I do not how all this links up.

Thanks
Ravi
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15634
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've written a blog post about CompletableFuture.

You can indeed use CompletableFuture to setup a chain of processing steps, and each of those steps can be processed asynchronously. However, it's not really simple to use, and there are a lot of possibilities, so you have to carefully study it and find out what is most appropriate for what you want to do.
 
s ravi chandran
Ranch Hand
Posts: 452
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:I've written a blog post about CompletableFuture.

You can indeed use CompletableFuture to setup a chain of processing steps, and each of those steps can be processed asynchronously. However, it's not really simple to use, and there are a lot of possibilities, so you have to carefully study it and find out what is most appropriate for what you want to do.

Thanks for the reply. I will check out the blog.

Meanwhile, is there any alternative that I can use do the same task?
 
s ravi chandran
Ranch Hand
Posts: 452
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a sample code I created :


This works fine.

My requirement is this:
1) N number of Suppliers will be present, will do some processing and return a result with get().
2) I wish to get parallel processing for all these suppliers and print "Task Complete" for each supplier when they finish task.

I am thinking of doing something like this:


in the main method:



Will this be truly parallel?

How can I reuse the task object?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic