top | item 40983474

(no title)

frant-hartm | 1 year ago

What do you mean by context switching?

My understanding is that virtual threads mostly eliminate context switching - for N CPUs JVM creates N platform threads and they run virtual threads as needed. There is no real context switching apart from GC and other JVM internal threads.

A platform thread picking another virtual thread to run after its current virtual thread is blocked on IO is not a context switch, that is an expensive OS-level operation.

discuss

order

giamma|1 year ago

The JVM will need to do context switching when reallocating the real thread that is running a blocked virtual thread to the next available virtual thread. It won't be CPU context switching, but context switching happens at the JVM level and represents an effort.

frant-hartm|1 year ago

Ok. This JVM-level switching is called mounting/un-mounting of the virtual thread and is supposed to be several orders of magnitude cheaper compared to normal context switch. You should be fine with millions of virtual threads.

anonymousDan|1 year ago

Does Java's implementation of virtual threads perform any kind of work stealing when a particular physical thread has no virtual threads to run (e.g. they are all blocked on I/O)?

mike_hearn|1 year ago

It does. They get scheduled onto the ForkJoinPool which is a work stealing pool.

immibis|1 year ago

"they run virtual threads as needed" - so when one virtual thread is no longer needed and another one is needed, they switch context, yes?

frant-hartm|1 year ago

This is called mounting/un-mounting and is much cheaper than a context switch.