File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Shutdown Threadpoolexecutor when queue is empty[SOLVED] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Shutdown Threadpoolexecutor when queue is empty[SOLVED]" Watch "Shutdown Threadpoolexecutor when queue is empty[SOLVED]" New topic
Author

Shutdown Threadpoolexecutor when queue is empty[SOLVED]

martin naughton
Ranch Hand

Joined: Sep 09, 2009
Posts: 30

Hello Guys,
I am changing over some code to use the concurrent api in java. at the moment it is using this Pooledexecutor. I want to use ThreadPoolExecutor

http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/PooledExecutor.html

One bit i like about it is the the method below. it will only shutdown when every thing is processed in the queue.

http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/PooledExecutor.html#shutdownAfterProcessingCurrentlyQueuedTasks%28%29

I was trying to look for the equvalent in the java concurrent API. I was looking at the shutdown method below.

java.util.concurrent.ThreadPoolExecutor

public void shutdown()

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.



I have searched all over the internet. I trying to understand by what they mean by "submitted task". Is it when all the task in pool will be finished and everything in the queue will be discarded or is it everything submitted in the queue will be finished including what is in the queue at the time the shutdown command is given?

Also await seems to only wait until all the tasks that were in the pool are finished. it does not care if any tasks are in the queue.

public boolean awaitTermination(long timeout,
TimeUnit unit)
throws InterruptedException

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

Does the threadpoolexecutor api support waiting for queue to be empty before shutting down? or do you have to implement a wait for the queue to be empty before calling shutdown yourself.

I do not mind implementing my own wait just that i want to understand the api completely. i would probably do a while statement waiting for the queue to be empty.

thanks for you help.


SCJA Done
SCJP Done
SCJD URLybird 1.3.1 Passed
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

martin naughton wrote:
java.util.concurrent.ThreadPoolExecutor

public void shutdown()

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.



I have searched all over the internet. I trying to understand by what they mean by "submitted task". Is it when all the task in pool will be finished and everything in the queue will be discarded or is it everything submitted in the queue will be finished including what is in the queue at the time the shutdown command is given?


Yes. Previous submitted tasks are tasks submitted into the pool prior to the shutdown call. And they will be executed before the pool threads terminates.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

martin naughton wrote:
Also await seems to only wait until all the tasks that were in the pool are finished. it does not care if any tasks are in the queue.

public boolean awaitTermination(long timeout,
TimeUnit unit)
throws InterruptedException

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

Does the threadpoolexecutor api support waiting for queue to be empty before shutting down? or do you have to implement a wait for the queue to be empty before calling shutdown yourself.


This method simply waits for termination -- whether termination occurs after the queue is drained or not depends on whether you requested an orderly shutdown or not.

Henry
martin naughton
Ranch Hand

Joined: Sep 09, 2009
Posts: 30

Hey henry,
thanks for clearing that up.





Yes. Previous submitted tasks are tasks submitted into the pool prior to the shutdown call. And they will be executed before the pool threads terminates.

So that means then that any thing in the queue is discarded. Understand

This method simply waits for termination -- whether termination occurs after the queue is drained or not depends on whether you requested an orderly shutdown or not.

this confuses me from what you said above. shutdown() is an orderly shutdown. it sounds like if do an request for an orderly shutdown through shutdown() that awaitTermination will wait until the queue is drained then terminate the executor? so it sounds like from your quote above that shutdown() will finish all tasks in the pool but then awaitTermination will say "hey shutdown do not terminate until everything in the queue is finished". correct?

an example would help

There are 10 tasks in the queue and 5 tasks in the thread pool. then shutdown() is called. then awaitTermination method is called. the 5 tasks in pool will be let finished and the code will wait at the awaitTermination line until all 5 tasks are finished then the executor will be terminated. so the 10 tasks in the queue will be discarded and never run. correct?

if this is so then i would have to do some thing like this




thanks for your help

Also serached the internet this morning for understanding the api. This post came up first in google. that was fast
http://www.google.ie/#hl=en&biw=1600&bih=990&q=threadpoolexecutor+shutdown+when+queue+is+empty&aq=f&aqi=&aql=&oq=&gs_rfai=&fp=ff8be12b9b06f6de
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

martin naughton wrote:
Yes. Previous submitted tasks are tasks submitted into the pool prior to the shutdown call. And they will be executed before the pool threads terminates.

So that means then that any thing in the queue is discarded. Understand



I don't understand why you are making such a strong distinction (separation) between a pool and it's work queue. The work queue is part of the pool, so when I say pool, I mean the whole pool, which includes the queue.

In order words, when you submit a task to the pool via the submit methods (or the any of the invoke and execute methods too), the task goes into the work queue, and blah blah blah, eventually being executed by one of the pool threads. So, "submitting a task" to the pool includes the work queue too. And "previously submitted tasks" includes tasks in the work queue too.

Henry
martin naughton
Ranch Hand

Joined: Sep 09, 2009
Posts: 30

that is what my exact confusion was. i thought the queue and pool were seperate.

thanks alot for clearing that up for me.

At least now the shutdown() method will exectue everyting in the queue. makes my implementation simpler. thanks alot and i hope this clears it up for anyone else.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shutdown Threadpoolexecutor when queue is empty[SOLVED]