Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ExecutorSerive vs CompletionService  RSS feed

 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not able to understand the difference between the two. In case if I use an ArrayList of Future to store the Future returned to me by submitting the tasks through ExecutorService, is it not same as CompletionService implementation?

Also, I need help to identify why the following code does not exit after rendering the page.
Code description: To render a page, there are 2 main tasks: render text and render image (in code, 5 in numbers). For test purpose, I was creating Image data as simple print statement. Each image consists of random number (max of 5) of such statements.

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amitabh mehra wrote:I am not able to understand the difference between the two. In case if I use an ArrayList of Future to store the Future returned to me by submitting the tasks through ExecutorService, is it not same as CompletionService implementation?


The CompletionService is basically a Queue which returns the Futures in the order which they complete. If you used an ArrayList for the same purpose you would have to look through each future and check if they are completed, then consume them. Of you would need to take each one in order, wait for it to complete, then do your work. The difference is that if you have 5 tasks, and they complete in the following order:
2, 1, 5, 4, 3

With the CompletionService you take() and wait for #2 to complete. Then do work on #2. Then you take() and get #1 - perhaps already done. You do the work on #1, then take() and get #5, etc... With the ArrayList you call #1's get() method which causes you to wait. While your consumer is sitting there doing nothing, #2 completes and sits there doing nothing. Then when #1 is done, you do your work, then get() #2, which is already done so do your work on it. Then you get() #3, which you have to wait for, and in the meantime #5 and #4 complete and sit doing nothing, wasting time.

So CompletionService is more efficient consumer Queue of the Futures since it returns the Futures as soon, and in the order in which they complete.


Also, I need help to identify why the following code does not exit after rendering the page.


The ExecutorService creates 3 new, non-Daemon threads. These Threads do not get killed when the tasks complete, they are pooled to be re-used if necessary. Because there are live, non-Daemon threads hanging around the application can't close. What you have to do is shutdown the ExecutorService . You have to be careful when you call it, though - because you don't want to shut down the service before all your tasks are submitted.
 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks... that cleared my doubts!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!