This repository was archived by the owner on Jun 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Use virtual threads by default on Java 21+ via Multi-Release JAR #60
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
caaf6d6
Initial plan
Copilot 4ae4200
Add virtual thread support on Java 21+ via Multi-Release JAR
Copilot ce4fd3d
Address code review feedback: use assertFalse, simplify README
Copilot af2540d
Update README.md
brunoborges 25829a4
Update src/test/java/com/github/copilot/sdk/ThreadFactoryProviderTest…
brunoborges fef1833
s
brunoborges 9d456f6
Remove unused LOG fields, use newSingleThreadExecutor for Java 21 ove…
Copilot e10745e
Potential fix for pull request finding 'Useless comparison test'
brunoborges 14b0ed9
Merge remote-tracking branch 'origin/main' into copilot/copilot-pr-60
edburns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/com/github/copilot/sdk/ThreadFactoryProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| package com.github.copilot.sdk; | ||
|
|
||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
|
|
||
| /** | ||
| * Provides thread factories for the SDK's internal thread creation. | ||
| * <p> | ||
| * On Java 17, this class returns standard platform-thread factories. On Java | ||
| * 21+, the multi-release JAR overlay replaces this class with one that returns | ||
| * virtual-thread factories, giving the SDK lightweight threads for its | ||
| * I/O-bound JSON-RPC communication without any user configuration. | ||
| * <p> | ||
| * The {@link java.util.concurrent.ScheduledExecutorService} used for | ||
| * {@code sendAndWait} timeouts in {@link CopilotSession} is <em>not</em> | ||
| * affected, because the JDK offers no virtual-thread-based scheduled executor. | ||
| * | ||
| * @since 0.2.2-java.1 | ||
| */ | ||
| final class ThreadFactoryProvider { | ||
|
|
||
| private ThreadFactoryProvider() { | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new daemon thread with the given name and runnable. | ||
| * | ||
| * @param runnable | ||
| * the task to run | ||
| * @param name | ||
| * the thread name for debuggability | ||
| * @return the new (unstarted) thread | ||
| */ | ||
| static Thread newThread(Runnable runnable, String name) { | ||
| Thread t = new Thread(runnable, name); | ||
| t.setDaemon(true); | ||
| return t; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a single-thread executor suitable for the JSON-RPC reader loop. | ||
| * | ||
| * @param name | ||
| * the thread name for debuggability | ||
| * @return a single-thread {@link ExecutorService} | ||
| */ | ||
| static ExecutorService newSingleThreadExecutor(String name) { | ||
| return Executors.newSingleThreadExecutor(r -> { | ||
| Thread t = new Thread(r, name); | ||
| t.setDaemon(true); | ||
| return t; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Returns {@code true} when this class uses virtual threads (Java 21+ | ||
| * multi-release overlay), {@code false} for platform threads. | ||
| * | ||
| * @return whether virtual threads are in use | ||
| */ | ||
| static boolean isVirtualThreads() { | ||
| return false; | ||
| } | ||
| } | ||
58 changes: 58 additions & 0 deletions
58
src/main/java21/com/github/copilot/sdk/ThreadFactoryProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| package com.github.copilot.sdk; | ||
|
|
||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
|
|
||
| /** | ||
| * Java 21+ override that uses virtual threads for the SDK's internal thread | ||
| * creation. | ||
| * <p> | ||
| * This class is placed under {@code META-INF/versions/21/} in the multi-release | ||
| * JAR and replaces the baseline {@code ThreadFactoryProvider} when running on | ||
| * Java 21 or later. | ||
| * | ||
| * @since 0.2.2-java.1 | ||
| */ | ||
| final class ThreadFactoryProvider { | ||
|
|
||
| private ThreadFactoryProvider() { | ||
| } | ||
|
Comment on lines
+20
to
+23
|
||
|
|
||
| /** | ||
| * Creates a new virtual thread with the given name and runnable. | ||
| * | ||
| * @param runnable | ||
| * the task to run | ||
| * @param name | ||
| * the thread name for debuggability | ||
| * @return the new (unstarted) virtual thread | ||
| */ | ||
| static Thread newThread(Runnable runnable, String name) { | ||
| return Thread.ofVirtual().name(name).unstarted(runnable); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a single-thread executor backed by a virtual-thread factory for the | ||
| * JSON-RPC reader loop. | ||
| * | ||
| * @param name | ||
| * the thread name for debuggability | ||
| * @return a single-thread virtual-thread-backed {@link ExecutorService} | ||
| */ | ||
| static ExecutorService newSingleThreadExecutor(String name) { | ||
| return Executors.newSingleThreadExecutor(Thread.ofVirtual().name(name).factory()); | ||
| } | ||
|
|
||
| /** | ||
| * Returns {@code true} — this is the virtual-thread overlay. | ||
| * | ||
| * @return {@code true} | ||
| */ | ||
| static boolean isVirtualThreads() { | ||
| return true; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.