0

i have a ThreadPool with a lot of threads. the task is finished if every thread of this threadpool is waiting on a BlockingQueue. how can i figure out if every Thread is waiting so i can shutdown the threadpool?

Exagon
  • 4,798
  • 6
  • 25
  • 53
  • How are you going to shut down the threadpool? – pvg Dec 13 '15 at 23:23
  • yeah thats another question... is there a possibility to terminate all waiting threads in a threadpool? or just to interrupt all threads in a threadpool? – Exagon Dec 13 '15 at 23:30
  • 1
    BlockingQueue docs _A BlockingQueue does not intrinsically support any kind of "close" or "shutdown" operation to indicate that no more items will be added. The needs and usage of such features tend to be implementation-dependent. For example, a common tactic is for producers to insert special end-of-stream or poison objects, that are interpreted accordingly when taken by consumers._ – pvg Dec 13 '15 at 23:31
  • 2
    so basically when you are done shoving processable items into the queue, you put in items that the receiving threads interpret as a signal to shut down. – pvg Dec 13 '15 at 23:32

1 Answers1

0

You should call shutdown() and then awaitTermination() with a long timeout value. When the tasks are done, it will shutdown.

See this answer: How to wait for all threads to finish, using ExecutorService?

Community
  • 1
  • 1
John Scattergood
  • 1,032
  • 8
  • 15