FULL PRODUCT VERSION :
1.6.0_07-b06
1.6.0_10-rc2-b32
ADDITIONAL OS VERSION INFORMATION :
tested on WinXP SP2 (5.1.2600) and Ubuntu Linux 2.6.24-19-generic
A DESCRIPTION OF THE PROBLEM :
As can be seen with example listed below, ThreadPoolExecutor with corePoolSize value lower than maxPoolSize will never try to add new threads beyond corePoolSize when tasks are submitted using unbounded queue.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
execute supplied snippet and examine output.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
new thread should be created if task is submitted and current poolSize is lower than maxPoolSize.
ACTUAL -
all tasks are executed by the same thread one-by-one.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.concurrent.*;
import java.util.Collection;
import java.util.ArrayList;
public class Main {
private static final int TASK_COUNT = 10;
public static void main(String[] args) {
final ThreadPoolExecutor tpe = new ThreadPoolExecutor(1, 5, 14, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(),new RejectedExecutionHandler()
{
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.out.println(r + " execution rejected");
}
});
final Collection<Task> tasks = new ArrayList<Task>();
for (int i = 0; i < TASK_COUNT; i++) {
tasks.add(new Task());
}
for (final Task task : tasks) {
synchronized(tpe) {
System.out.println(Thread.currentThread().getName() + " submitted task");
System.out.println("poolsize = " + tpe.getPoolSize() + "; tasksCount = " + tpe.getTaskCount() );
tpe.execute(task);
}
}
}
}
class Task implements Runnable {
public void run() {
System.out.println(this + " is being executed in " + Thread.currentThread().getName() + ": " + Thread.currentThread().hashCode());
System.out.println(Thread.getAllStackTraces().size());
try {
Thread.sleep(500);
System.out.println(this + " done executing in " + Thread.currentThread().getName() + ": " + Thread.currentThread().hashCode());
} catch (InterruptedException e) {
System.exit(-1);
e.printStackTrace();
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
always create ThreadPoolExecutor with corePoolSize equal to maxPoolSize
|