A Growing ThreadPool

I'd like to create a threadpool that will grow the number of threads to max and then shrink back down if the threads aren't used for a period of time.

This is described in the javadoc documentation, but I couldn't get it to work with my test. It will only run 5 threads at a time.

I tried the 3 different ways of creating a ThreadPoolExecutor.

I didn't have any luck with any of them.

Thanks,

Scott

publicclass ThreadTestimplements Runnable{

publicstaticvoid main(String[] args)

{

ThreadPoolExecutor executor =new ThreadPoolExecutor(5, 10, 300, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(Integer.MAX_VALUE), Executors.defaultThreadFactory());

//ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 300, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(50), Executors.defaultThreadFactory());

//ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 300, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), Executors.defaultThreadFactory());

for(int i=0;i<3;i++)

{

ThreadTest test =new ThreadTest();

test.setExecutor(executor);

new Thread(test).start();

}

try{

executor.awaitTermination(10000, TimeUnit.MILLISECONDS);

}catch (InterruptedException e){

e.printStackTrace();

}

System.out.println(executor.getCorePoolSize());

System.out.println(executor.getMaximumPoolSize());

System.out.println(executor.getLargestPoolSize());

executor.shutdown();

}

publicvoid run(){

for(int i=0;i<5;i++)

executor.submit(new Runnable(){

publicvoid run(){

try{

System.out.println("started sleeping");

Thread.sleep(3000);

System.out.println("stopped sleeping");

}catch (InterruptedException e){

e.printStackTrace();

}

}

});

}

private ThreadPoolExecutor executor;

public ThreadPoolExecutor getExecutor(){

return executor;

}

publicvoid setExecutor(ThreadPoolExecutor executor){

this.executor = executor;

}

}

[3823 byte] By [scarcher2a] at [2007-11-15]