| Author |
Logic of Thread Pools
|
Hal Taylor
Greenhorn
Joined: May 01, 2004
Posts: 12
|
|
I'm struggling a bit to fully understand the implementation of thread pools. I grasp the meta-level concepts behind the thread pool (thread creation is expensive, so create a batch of them in advance and have them pick up tasks out of an incoming queue). I get a little lost in the implementation details, however. I reviewed a code example which had a "Done" object, and I don't quite get it. Could somebody walk me through the flow of a thread pool and explain the necessary parts to implement? Thanks in advance.
|
 |
Alexandru Popescu
Ranch Hand
Joined: Jul 12, 2004
Posts: 995
|
|
Maybe looking at Doug Lea PooledExecutor will help you. He documents it very well. ./pope
|
blog - InfoQ.com
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
I also found the Apache Commons Threadpool an interesting read. The core to both of these (to me anyhow) was a blocking queue. Clients put commands to be executed (runnables) into a queue. Some number of threads pick commmands out of the queue in FIFO order and execute them. The blocking bit is important when the queue is empty ... threads block until something appears in the queue and then proceed. It can also be important when the queue is full ... clients may block until there is room to insert the item. The blocking queues in Java 5.0 are wonderfully tunable for how big to grow, what to do when full and so on.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
thread creation is expensive, so create a batch of them in advance and have them pick up tasks out of an incoming queue
It is actually a bit more complex than that, it is a matter of throughput. On average, it may be quicker to service clients by servicing a limited number of them, in order, than to service them all simultaneously. A good case for a threadpool, is to actually limit the number of available threads. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
 |
|
|
subject: Logic of Thread Pools
|
|
|