chore: add a helper method to create a dedicated virtual thread scheduler.#164
Merged
lihaoyi merged 1 commit intocom-lihaoyi:masterfrom Feb 2, 2025
Merged
chore: add a helper method to create a dedicated virtual thread scheduler.#164lihaoyi merged 1 commit intocom-lihaoyi:masterfrom
lihaoyi merged 1 commit intocom-lihaoyi:masterfrom
Conversation
lihaoyi
reviewed
Jan 22, 2025
| maximumPoolSize: Int, | ||
| keepAliveTime: Int, | ||
| timeUnit: TimeUnit): ForkJoinPool = { | ||
| new ForkJoinPool( |
Member
There was a problem hiding this comment.
Does it need to be a ForkJoinPool, or can other pools work? I've had odd interactions between ForkJoinPool and other libraries (e.g. scala.util.concurrent.blocking) so I wonder if we can provide alternative pools
Contributor
Author
There was a problem hiding this comment.
No, it can be any Executor, just make sure a virtual thread can be always run. We can use Executor#newCachedThreadPool, or ThreadPoolExecutor with a larger max pool size (because of the classloading issue) if we have a small thread pool size, and then the Virtual thread may not be able to run, then deadlock can happen, I have updated the method name.
This help method is just integrated with the CarrierThreadFactory.
see: https://openjdk.org/jeps/491
Future Work
There are a few remaining cases, unrelated to the synchronized keyword, in which a virtual thread cannot unmount when blocking:
When resolving a symbolic reference ([JVMS §5.4.3](https://docs.oracle.com/javase/specs/jvms/se23/html/jvms-5.html#jvms-5.4.3)) to a class or interface and the virtual thread blocks while loading a class. This is a case where the virtual thread pins the carrier due to a native frame on the stack.
When blocking inside a class initializer. This is also a case where the virtual thread pins the carrier due to a native frame on the stack.
When waiting for a class to be initialized by another thread ([JVMS §5.5](https://docs.oracle.com/javase/specs/jvms/se23/html/jvms-5.html#jvms-5.5)). This is a special case where the virtual thread blocks in the JVM, thus pinning the carrier.
These cases should rarely cause issues but we will revisit them if they prove to be problematic.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation:
Add a helper method to create a separate virtual thread scheduler.