-
Notifications
You must be signed in to change notification settings - Fork 3
Use virtual threads by default on Java 21+ via Multi-Release JAR #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
caaf6d6
4ae4200
ce4fd3d
af2540d
25829a4
fef1833
9d456f6
e10745e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } | ||
| } | ||
| 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; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.