Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

question about Doung Lea's thread pool  RSS feed

zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
recently,i am studying the Doung lea's thread pool.and plan introduce it to my projects,i write following code to test the "PooledExecutor":

public class MyTestClient {
public static void main(String args[]){
PooledExecutor executor=new PooledExecutor(new BoundedBuffer(10),5);
for(int i=0;i<40;i++){
executor.execute(new WorkerThread());
}catch(Exception e){
class WorkerThread extends Thread{
public void run(){
System.out.print(this.getName()+" is running!!\r\n");
}catch(Exception e){

the output as following:

Thread-0 is running!!
Thread-2 is running!!
Thread-16 is running!!
Thread-14 is running!!
Thread-20 is running!!
Thread-18 is running!!
Thread-4 is running!!
Thread-5 is running!!
Thread-6 is running!!
Thread-7 is running!!
Thread-26 is running!!
Thread-8 is running!!
Thread-9 is running!!
Thread-10 is running!!
Thread-11 is running!!
Thread-13 is running!!
Thread-32 is running!!
Thread-12 is running!!
Thread-21 is running!!
Thread-22 is running!!
Thread-23 is running!!
Thread-25 is running!!
Thread-38 is running!!
Thread-24 is running!!
Thread-27 is running!!
Thread-28 is running!!
Thread-29 is running!!
Thread-44 is running!!
Thread-31 is running!!
Thread-30 is running!!
Thread-33 is running!!
Thread-34 is running!!
Thread-37 is running!!
Thread-36 is running!!
Thread-35 is running!!
Thread-39 is running!!
Thread-40 is running!!
Thread-43 is running!!
Thread-42 is running!!
Thread-41 is running!!

as you can see that all of the thread are new created (because all the names of the threads are different). i think the main functionality of thread pool should be REUSE existing thread,but the "PooledExecutor" can't,maybe i didn't use it correctly,who can give me a answer.

thank you for any helps
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you misunderstand how a thread pool works. The threads you are creating are being used as ordinary Runnables. The pool is in charge of creating threads, not you. Take a look at this code:

On my computer this code starts about 13 threads to handle 40 tasks. If the tasks were longer it might start 40, though -- by default PooledExecutor will allocate a thread whenever it needs one but doesn't have a spare to use. If you pass an integer argument to the PooledExecutor constructor that will impose a limit on the number of threads used at a time.
[ August 19, 2004: Message edited by: David Weitzman ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!