Skip to content
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

BlockingRunner 增加对虚拟线程的配置支持 #744

Merged
merged 4 commits into from
Sep 25, 2023

Conversation

ForliyScarlet
Copy link
Member

@ForliyScarlet ForliyScarlet commented Sep 25, 2023

现在添加JVM参数 -Dsimbot.runInBlocking.dispatcher=virtual 即可将所有通过 BlockingRunner 执行的阻塞函数(xxxBlocking)的调度器切换至虚拟线程。

(你也可以在 #743 中通过 -Dsimbot.runInBlocking.dispatcher=custom 切换到其他自定义调度器 )

Note
更多描述参考 API文档

不过需要注意的是,这里切换到的是 xxxBlocking 的逻辑阻塞,而此函数本身可能会被某个异步的虚拟线程 "join"。因此当开启BlockingRunner的虚拟线程调度器时,我们建议你将事件调度时使用的调度器也切换为虚拟线程。否则,在事件调度过程中仍会因物理线程被虚拟线程阻塞而导致影响效率。

在普通的core模块应用中,你可以通过 ApplicationConfigurationSimpleListenerManagerConfiguration 的中的 coroutineContext 来定制事件调度器。

Spring Starter 中,向程序中注入一个 CoroutineDispatcherContainer 的实例并使其携带一个虚拟线程的线程池。

例如Java中:

@Bean
public CoroutineDispatcherContainer virtualCoroutineDispatcherContainer() {
 return new CoroutineDispatcherContainer(ExecutorsKt.from(Executors.newVirtualThreadPerTaskExecutor()));
}

@ForliyScarlet ForliyScarlet added the 特性 一个新特性,或者一个新的功能/能力 label Sep 25, 2023
@ForteScarlet ForteScarlet merged commit 69e1ff9 into v3-dev Sep 25, 2023
3 of 4 checks passed
@ForteScarlet ForteScarlet deleted the dev/blockingRunner-virtual-thread-support branch September 26, 2023 00:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
特性 一个新特性,或者一个新的功能/能力
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants