Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can I get notified when my runnables execution completes when using Java 5 ThreadPool  RSS feed

 
Prashant R Kataria
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using java.util.concurrent.ThreadPoolExecutor to implement my Thread pool. I am submitting a runnable and getting Future object to track the runnable's execution.

But Requirement is, when execution of my Runnable ends I should get intimation.

Is it possible using java 5 or if someone knows about any other library then please inform me.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Extend ThreadPoolExecutor and override afterExecute() method. This method gets called immediately after a runnable has finished execution and before a new task is picked up by that thread pool worker.
 
Nicole Lacoste
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

You could try using a CompletionService. I've never done this just read about it in Java Concurrency in Practice by Goetz. To quote Goetz, "CompletionService combines the functionality of an Executor and a BlockingQueue. You can submitt Callable tasks to it for execution and use the queue-like methods take and poll to retieve completed results, packaged as futures, as they are available".

Take a look at the java docs for the completion service.
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/CompletionService.html

Nicole
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Nicole,
Yeah that is one of the option, but the choice will probably depend on the fact whether the user wants to wait for result or wants to get a callback when the job is done.
Completion service gives the option of waiting for the result, whereas overriding afterExecute() gives a callback.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!