-
Bug
-
Resolution: Unresolved
-
P4
-
None
-
8, 11, 17, 21, 23, 24, 25
-
generic
-
generic
ADDITIONAL SYSTEM INFORMATION :
OpenJDK 23.0.2, Java 8, Java 18 tested as well. Reproduced on Windows 10 and Linux.
A DESCRIPTION OF THE PROBLEM :
For specific case for ThreadPoolExecutor with queue capacity = 1 and corePoolSize = 1 it can happen that
after all threads are initialized till maxPoolSize and all tasks are finished, running the same new bunch of tasks cause RejectedExecutionException.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create executor with corePoolSize = 1 (it is important), maximumPoolSize = 10 (it can be different).
As queue should be LinkedBlockingQueue with capacity = 1 (it is important).
Keep alive time should be enough to run second time bunch of tasks using created threads.
Run 11 tasks first time.
Wait until all tasks are finished.
Run 11 tasks second time.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
All 11 second group tasks are running without issues.
ACTUAL -
Some of 11 second group tasks are failing with RejectedExecutionException.
---------- BEGIN SOURCE ----------
package org.example;
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) {
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(1);
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 10, 60, TimeUnit.SECONDS, queue);
// Run 11 threads = maxPoolSize + queueCapacity (1)
run11Threads(executor, "First ");
sleep("wait");
// wait until the end
while (executor.getActiveCount() > 0);
System.out.println("First 11 finished");
run11Threads(executor, "Second ");
}
private static void run11Threads(ThreadPoolExecutor executor, String name) {
for (int i = 0; i < 11; i++) {
int finalI = i;
try {
executor.execute(() -> sleep(name + finalI));
} catch (RejectedExecutionException e) {
System.out.println(name + finalI + " rejected");
}
}
}
private static void sleep(String name) {
try {
System.out.println("Start " + name);
Thread.sleep(1_000);
System.out.println("End " + name);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
---------- END SOURCE ----------
OpenJDK 23.0.2, Java 8, Java 18 tested as well. Reproduced on Windows 10 and Linux.
A DESCRIPTION OF THE PROBLEM :
For specific case for ThreadPoolExecutor with queue capacity = 1 and corePoolSize = 1 it can happen that
after all threads are initialized till maxPoolSize and all tasks are finished, running the same new bunch of tasks cause RejectedExecutionException.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create executor with corePoolSize = 1 (it is important), maximumPoolSize = 10 (it can be different).
As queue should be LinkedBlockingQueue with capacity = 1 (it is important).
Keep alive time should be enough to run second time bunch of tasks using created threads.
Run 11 tasks first time.
Wait until all tasks are finished.
Run 11 tasks second time.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
All 11 second group tasks are running without issues.
ACTUAL -
Some of 11 second group tasks are failing with RejectedExecutionException.
---------- BEGIN SOURCE ----------
package org.example;
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) {
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(1);
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 10, 60, TimeUnit.SECONDS, queue);
// Run 11 threads = maxPoolSize + queueCapacity (1)
run11Threads(executor, "First ");
sleep("wait");
// wait until the end
while (executor.getActiveCount() > 0);
System.out.println("First 11 finished");
run11Threads(executor, "Second ");
}
private static void run11Threads(ThreadPoolExecutor executor, String name) {
for (int i = 0; i < 11; i++) {
int finalI = i;
try {
executor.execute(() -> sleep(name + finalI));
} catch (RejectedExecutionException e) {
System.out.println(name + finalI + " rejected");
}
}
}
private static void sleep(String name) {
try {
System.out.println("Start " + name);
Thread.sleep(1_000);
System.out.println("End " + name);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
---------- END SOURCE ----------
- relates to
-
JDK-8241094 Updating core poolsize of a ThreadPoolExecutor throws RejectedExecutionException
-
- Open
-