- Loading...
Virtual threads are The easiest way to get started is to configure your IDE to use a recent Project Loom Early Access (EA) build and get familiar with using the java.lang.Thread API to create a virtual thread to execute some code. Virtual Threads are just threads that are scheduled by the Java virtual machine rather than the operating system. They don't have their own API, instead the java.lang.Thread API has been updated to support the creation of virtual They are suited to executing code that spends most of its time blocked, maybe waiting for a data to arrive on a network socket. Virtual threads are not suited to running code that is compute bound.
In addition to to the Thread API, the java.util.concurrent.ExecutorService and Executors APIs are have been updated to make it easy to work with virtual threads. Unlike traditional threads pools, virtual threads are cheap enough that a new virtual thread can be created for each task, no need for pooling of threads.
The following uses a static factory method to create a virtual thread, invokes its start method to schedule it, and then invokes the join method to wait up to 5 seconds for the thread to terminate.
...