ForkJoinPool of JDK21 : Despite having an idle worker, the submitted task did not run

徐佳 75317544 at qq.com
Wed Oct 22 22:45:14 UTC 2025


Hi,
Despite having an idle worker, the submitted task did not run.
Steps to reproduce:-------------------1. before all , add a breakpoint on the 1864th line of ForkJoinPool source code in awaitWork method.2. then debug the following code,and a thread will be paused at breakpoint3. after "submited task2" printed in the console, resume the breakpoint.4. Despite having an idle worker, task2 did not run.

public class ForkJoinPoolTest {

	public static void main(String[] args) throws InterruptedException {

		//Add a breakpoint on the 1864th line of ForkJoinPool source code in awaitWork method.

		

		ForkJoinPool pool = new ForkJoinPool(2);//total 2 Threads

		

		//create worker w1, keep running and printing pool information

		pool.submit(() -> {

			while(true) {

				System.err.println("===================");

				System.err.println("QueuedSubmissionCount:" + pool.getQueuedSubmissionCount());

				System.err.println("ActiveThreadCount:" + pool.getActiveThreadCount());

				System.err.println("PoolSize:" + pool.getPoolSize());

				System.err.println("===================");

				

				LockSupport.parkNanos(5 * 1000_000_000L);//5s

			}

		});

		

		//create another worker w2

		pool.submit(() -> System.out.println(Thread.currentThread().getName() + " : finished task1"));

		System.out.println("submited task1");

		

		Thread.sleep(3 * 1000);// waiting for task1 to end

		//now task1 finished and no task in queue, w2 is pausing at breakpoint in  awaitWork method.

		

		pool.submit(() -> System.out.println(Thread.currentThread().getName() + " : finished task2"));

		System.out.println("submited task2");

		//after submitted task2, resume w2 , and then w2 will park on line 1891 ,although there is still task2 in the Submission queue 

		

		//if no new task submitted, task2 will not run.

		Thread.sleep(5 * 60 * 1000);

		pool.close();

	}




}

result:

 Environment: ------------ 
java version "21.0.8" 2025-07-15 LTS

Java(TM) SE Runtime Environment (build 21.0.8+12-LTS-250)

Java HotSpot(TM) 64-Bit Server VM (build 21.0.8+12-LTS-250, mixed mode, sharing)

it seams that a task submitted between method “scan”and “awaitWork” may not be executed immediatelyI think the reason is that the worker did not recheck the waiting condition "hasTasks" between "enqueue" and "park"I don't know whether this is a bug or it's just designed this way Please let me know if more information is needed. Thanks, Jia Xu


徐佳
75317544 at qq.com



 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20251023/d48120af/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 5E110CCA at B2062160.7A5EF968.jpg
Type: image/jpeg
Size: 155320 bytes
Desc: not available
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20251023/d48120af/5E110CCAB2062160.7A5EF968-0001.jpg>


More information about the core-libs-dev mailing list