• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

ExecutorSerive vs CompletionService

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks... that cleared my doubts!
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic