-
Notifications
You must be signed in to change notification settings - Fork 3
Use virtual threads by default on Java 21+ #56
Copy link
Copy link
Open
Description
Summary
When running on Java 21+, the SDK should default to using virtual threads instead of platform threads for its internal thread pools and thread creation.
Motivation
Virtual threads (JEP 444, finalized in Java 21) are lightweight threads that can significantly reduce resource usage for I/O-bound workloads like the JSON-RPC communication this SDK performs. Since the SDK targets Java 17+, we can use Multi-Release JARs (JEP 238) to provide a Java 21+ implementation that uses virtual threads while keeping the Java 17 baseline implementation with platform threads.
Current thread usage
The SDK currently creates platform threads in several places:
JsonRpcClient—Executors.newSingleThreadExecutor()for the JSON-RPC reader loop, plus aThread(r, "jsonrpc-reader")factoryCopilotSession—ScheduledExecutorServiceforsendAndWaittimeouts, plusThread(r, "sendAndWait-timeout")factoryCliServerManager—Threadfor stderr forwarding
Proposed approach
- Create a
ThreadFactoryProvider(or similar) abstraction insrc/main/java17/that returns standard platform-thread factories - Create a Java 21+ override in
src/main/java21/that returns virtual-thread factories (Thread.ofVirtual().factory()) - Configure the Maven build to produce a multi-release JAR with the Java 21 classes under
META-INF/versions/21/ - Replace direct
new Thread()andExecutors.newSingleThreadExecutor()calls with the factory abstraction
Documentation
Ensure that the following documentation is updated to clearly describe virtual threads support when running on Java 21+:
- README.md — Add a section or note about automatic virtual thread usage on Java 21+, and any configuration/opt-out if applicable
- Quick Start guide (
src/site/markdown/) — Mention virtual threads as a benefit of running on Java 21+ in the getting started flow - Examples (e.g.,
jbang-example.java) — Ensure examples work seamlessly on both Java 17 (platform threads) and Java 21+ (virtual threads), and add comments or notes highlighting the behavior difference - Site docs (
src/site/markdown/) — Add a dedicated section or page covering virtual threads support: what it means for users, performance implications, and any caveats (e.g.,ScheduledExecutorServiceremaining on platform threads) - Javadoc — Document thread behavior on relevant public APIs (e.g.,
CopilotClient,CopilotSession) so users understand which threads are used at runtime
Notes
- The
ScheduledExecutorServiceinCopilotSessiondoes not have a virtual-thread equivalent in the JDK — it may need to stay as-is or use an alternative scheduling approach - Virtual threads should use the default
ForkJoinPoolcarrier; no custom carrier pool is needed - Ensure thread names are preserved for debuggability (
Thread.ofVirtual().name("jsonrpc-reader"))
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Type
Fields
Give feedbackNo fields configured for issues without a type.