Skip to content

Commit 32da3be

Browse files
Copilotslachiewicz
andcommitted
Bound Java 21 virtual thread pool to prevent unbounded ThreadLocal accumulation
Co-authored-by: slachiewicz <[email protected]>
1 parent bb9d08f commit 32da3be

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/main/java21/org/codehaus/plexus/archiver/zip/ConcurrentJarCreatorExecutorServiceFactory.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@
1919

2020
import java.util.concurrent.ExecutorService;
2121
import java.util.concurrent.Executors;
22+
import java.util.concurrent.LinkedBlockingQueue;
2223
import java.util.concurrent.ThreadFactory;
24+
import java.util.concurrent.ThreadPoolExecutor;
25+
import java.util.concurrent.TimeUnit;
2326
import java.util.concurrent.atomic.AtomicInteger;
2427

2528
/**
26-
* Post Java 21 implementation. Create one Virtual Thread per task execution. Apply same thread names as well.
29+
* Post Java 21 implementation. Uses virtual threads with a bounded thread pool to prevent
30+
* unbounded ThreadLocal accumulation while maintaining the benefits of virtual threads.
2731
*
2832
* @since 4.10.1
2933
*/
@@ -38,6 +42,10 @@ static ExecutorService createExecutorService(int poolSize) {
3842
.name("plx-arch-" + poolCount + "-" + threadCounter.incrementAndGet())
3943
.unstarted(r);
4044
};
41-
return Executors.newThreadPerTaskExecutor(threadFactory);
45+
// Use poolSize as both core and max to prevent unbounded thread growth
46+
// Even with virtual threads, we need to limit concurrent threads to prevent
47+
// unbounded ThreadLocal accumulation across multiple jar creation operations
48+
return new ThreadPoolExecutor(
49+
poolSize, poolSize, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), threadFactory);
4250
}
4351
}

0 commit comments

Comments
 (0)