chore: add a helper method to create a dedicated virtual thread scheduler.#164
chore: add a helper method to create a dedicated virtual thread scheduler.#164lihaoyi merged 1 commit intocom-lihaoyi:masterfrom
Conversation
| maximumPoolSize: Int, | ||
| keepAliveTime: Int, | ||
| timeUnit: TimeUnit): ForkJoinPool = { | ||
| new ForkJoinPool( |
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
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.
Motivation:
Add a helper method to create a separate virtual thread scheduler.