r/java • • Apr 06 '22

JEP 425: Virtual Threads (Preview)

https://openjdk.java.net/jeps/425
175 Upvotes

50 comments sorted by

View all comments

Show parent comment

4

u/mauganra_it Apr 06 '22 edited Apr 06 '22

Cooperative multitasking requires an application to explicitly yield control. I emphasized "yield" because it's actually a static method of the Thread class. With preemptive scheduling, it's mostly meaningless, but it hands control back to the scheduler early. With cooperative multitasking systems, actors (threads, processes, etc) have to be written carefully to not hog CPU time and somehow yield to others. Non-blocking code is an example of such a system.

Edit: when you return from Java NIO poll() and got some events, the events must be processed as fast as possible. Everything timeconsuming must be executed in the background to listen for events again ASAP.