aspose file tools*
The moose likes Threads and Synchronization and the fly likes question about Doung Lea's thread pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "question about Doung Lea Watch "question about Doung Lea New topic
Author

question about Doung Lea's thread pool

zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
hello
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);
executor.setMinimumPoolSize(2);
executor.setKeepAliveTime(-1);
try{
for(int i=0;i<40;i++){
executor.execute(new WorkerThread());
}
}catch(Exception e){
e.printStackTrace();
}
}
}
class WorkerThread extends Thread{
public void run(){
try{
Thread.sleep(5000);
System.out.print(this.getName()+" is running!!\r\n");
}catch(Exception e){
e.printStackTrace();
}
}
}




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

Joined: Jul 27, 2001
Posts: 1365
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about Doung Lea's thread pool