• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Shutdown Threadpoolexecutor when queue is empty[SOLVED]

 
Ranch Hand
Posts: 30
MyEclipse IDE VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
MyEclipse IDE VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
MyEclipse IDE VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic