Use virtual threads by default on Java 21+ via Multi-Release JAR#60
Use virtual threads by default on Java 21+ via Multi-Release JAR#60
Conversation
Agent-Logs-Url: https://github.com/github/copilot-sdk-java/sessions/e74714e4-bb84-4af7-b1ef-44f576c5b37c Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
Agent-Logs-Url: https://github.com/github/copilot-sdk-java/sessions/e74714e4-bb84-4af7-b1ef-44f576c5b37c Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR introduces a multi-release JAR approach to automatically use virtual threads on Java 21+ for the SDK’s internal I/O threading while keeping Java 17 as the baseline runtime.
Changes:
- Add a
ThreadFactoryProviderabstraction insrc/main/java/and a Java 21+ override insrc/main/java21/to switch internal threads to virtual threads via MR-JAR. - Update build configuration (manifest
Multi-Release: true, compilesrc/main/java21on JDK 21+ profile, Spotless includes). - Update internal thread creation call sites and refresh docs/examples to describe virtual thread behavior.
Show a summary per file
| File | Description |
|---|---|
pom.xml |
Enables MR-JAR manifest entry; compiles Java 21 sources under the JDK 21+ profile; expands Spotless includes to cover src/main/java21. |
src/main/java/com/github/copilot/sdk/ThreadFactoryProvider.java |
Baseline (Java 17) thread/executor factory implementation using platform daemon threads. |
src/main/java21/com/github/copilot/sdk/ThreadFactoryProvider.java |
Java 21+ MR-JAR overlay switching to virtual threads / virtual-thread executors. |
src/main/java/com/github/copilot/sdk/JsonRpcClient.java |
Routes JSON-RPC reader loop executor creation through ThreadFactoryProvider. |
src/main/java/com/github/copilot/sdk/CliServerManager.java |
Routes CLI stderr forwarding thread creation through ThreadFactoryProvider. |
src/test/java/com/github/copilot/sdk/ThreadFactoryProviderTest.java |
Adds unit tests for the provider. |
src/main/java/com/github/copilot/sdk/CopilotClient.java |
Javadoc update documenting Java 21+ virtual thread behavior and scheduler exception. |
src/main/java/com/github/copilot/sdk/CopilotSession.java |
Javadoc update clarifying scheduled timeout threads remain platform threads. |
README.md |
Updates requirements wording and adds a “Virtual Threads” section. |
src/site/markdown/index.md |
Recommends Java 21+ for automatic virtual thread support. |
src/site/markdown/getting-started.md |
Recommends Java 21+ for automatic virtual thread support. |
src/site/markdown/advanced.md |
Adds an advanced “Virtual Threads (Java 21+)” section. |
jbang-example.java |
Notes automatic virtual thread usage on Java 21+. |
Copilot's findings
- Files reviewed: 13/13 changed files
- Comments generated: 6
src/test/java/com/github/copilot/sdk/ThreadFactoryProviderTest.java
Outdated
Show resolved
Hide resolved
| final class ThreadFactoryProvider { | ||
|
|
||
| private static final Logger LOG = Logger.getLogger(ThreadFactoryProvider.class.getName()); | ||
|
|
||
| private ThreadFactoryProvider() { | ||
| } |
There was a problem hiding this comment.
LOG is declared but never used in this class. Please remove it (and any now-unneeded logging import) to avoid dead code and future confusion.
There was a problem hiding this comment.
Removed unused LOG field and java.util.logging.Logger import from both ThreadFactoryProvider files (baseline and Java 21 overlay). Also fixed the Java 21 overlay to use Executors.newSingleThreadExecutor(...) instead of newThreadPerTaskExecutor(...) for a true single-thread executor, and replaced the empty verification section in advanced.md with actionable guidance. See commit 9d456f6.
| final class ThreadFactoryProvider { | ||
|
|
||
| private static final Logger LOG = Logger.getLogger(ThreadFactoryProvider.class.getName()); | ||
|
|
||
| private ThreadFactoryProvider() { | ||
| } |
There was a problem hiding this comment.
LOG is declared but never used in this class. Please remove it (and any now-unneeded logging import) to avoid dead code and future confusion.
src/main/java21/com/github/copilot/sdk/ThreadFactoryProvider.java
Outdated
Show resolved
Hide resolved
src/site/markdown/advanced.md
Outdated
| ### Verifying Virtual Thread Usage | ||
|
|
||
| You can check at runtime whether the SDK is using virtual threads: | ||
|
|
||
| ```java | ||
| // Thread names are preserved for debuggability regardless of thread type. | ||
| // On Java 21+, the jsonrpc-reader and cli-stderr-reader threads will be virtual. | ||
| ``` |
There was a problem hiding this comment.
This section claims users can verify virtual thread usage but the code block is empty, and ThreadFactoryProvider is package-private so it can’t be referenced from user code anyway. Either provide a concrete, user-accessible verification method/example (e.g., documented logging output / thread dump indicators), or remove the section.
There was a problem hiding this comment.
Already addressed in commit 9d456f6 — replaced the empty code block with concrete verification guidance (using jcmd thread dumps and IDE thread inspectors), and removed the reference to the package-private ThreadFactoryProvider class.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
….java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…rlay, fix empty docs section Agent-Logs-Url: https://github.com/github/copilot-sdk-java/sessions/46706dd3-402c-41c9-9150-3cfae7db8c3f Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
Resolves #98
Before the change?
new Thread()andExecutors.newSingleThreadExecutor()calls, regardless of JVM version.After the change?
Core abstraction:
ThreadFactoryProviderinsrc/main/java/— baseline returning platform threadsThreadFactoryProviderinsrc/main/java21/— overlay usingThread.ofVirtual().name(...)withExecutors.newSingleThreadExecutor()backed by a virtual-thread factoryCall sites updated:
JsonRpcClient— reader executor now viaThreadFactoryProvider.newSingleThreadExecutor("jsonrpc-reader")CliServerManager— stderr thread now viaThreadFactoryProvider.newThread(..., "cli-stderr-reader")CopilotSessionScheduledThreadPoolExecutor— unchanged (no virtual-thread scheduled executor in JDK)Build:
Multi-Release: truemanifest entrymaven-compiler-pluginexecution with--release 21+multiReleaseOutput=truesrc/main/java21/**/*.javaCode review fixes:
LOGfield andjava.util.logging.Loggerimport from bothThreadFactoryProviderclassesExecutors.newSingleThreadExecutor(Thread.ofVirtual().name(name).factory())instead ofnewThreadPerTaskExecutor(...)to provide a true single-thread executor matching the method contractadvanced.mdwith actionable guidance (usingjcmdthread dumps and IDE thread inspectors)Docs: README, site docs (index, getting-started, advanced), jbang example, Javadoc on
CopilotClient/CopilotSession.Pull request checklist
mvn spotless:applyhas been run to format the codemvn clean verifypasses locallyDoes this introduce a breaking change?