Week 43 — What is a virtual thread and how is it different from a platform thread?

Question of the Week #43
What is a virtual thread and how is it different from a platform thread?
4 Replies
Eric McIntyre
Eric McIntyre14mo ago
Threads allow executing multiple sequences of code in parallel and are typically managed by the operating system. However, there are memory requirements for each thread and OS limitations on the maximum number of threads. On the other hand, virtual threads are lightweight threads that are managed by the JVM. Virtual threads still use OS threads for running the code but when a virtual thread executes a blocking call, it is detached from the OS thread and the OS thread can be used for a different virtual thread. When the blocking operation completes, the virtual thread is assigned to an OS thread again and can continue running. Since virtual threads are more lightweight compared to platform (OS) threads, it is possible to create many more virtual threads than OS threads with less memory and without running into OS limits on the number of threads. It is possible to create a platform thread like the following:
new Thread(()->{
System.out.println("I am running in a platform thread: "+Thread.currentThread());
try{
Thread.sleep(1_000);//this blocks the thread
}catch(InterruptedException e){
Thread.currentThread().interrupt();
}
System.out.println("still running after 1 seconds");
}).start();
new Thread(()->{
System.out.println("I am running in a platform thread: "+Thread.currentThread());
try{
Thread.sleep(1_000);//this blocks the thread
}catch(InterruptedException e){
Thread.currentThread().interrupt();
}
System.out.println("still running after 1 seconds");
}).start();
Similarly, a virtual thread can be created as follows:
Thread.ofVirtual().start(()->{
System.out.println("I am running in a virtual thread: "+Thread.currentThread());
try{
Thread.sleep(1_000);//while the virtual thread is blocked, other virtual threads can reuse the platform (OS) thread
}catch(InterruptedException e){
Thread.currentThread().interrupt();
}
System.out.println("still running after 1 seconds");
});
Thread.ofVirtual().start(()->{
System.out.println("I am running in a virtual thread: "+Thread.currentThread());
try{
Thread.sleep(1_000);//while the virtual thread is blocked, other virtual threads can reuse the platform (OS) thread
}catch(InterruptedException e){
Thread.currentThread().interrupt();
}
System.out.println("still running after 1 seconds");
});
Aside from Thread.ofVirtual(), virtual threads can also be created using an ExecutorService. Specifically, Executors.newVirtualThreadPerTaskExecutor() returns an Executor that creates a new virtual thread for each task that is submitted to it using ExecutorService#execute:
ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
executor.execute(()->{
System.out.println("I am running in a platform thread: "+Thread.currentThread());
});
ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
executor.execute(()->{
System.out.println("I am running in a platform thread: "+Thread.currentThread());
});
Eric McIntyre
Eric McIntyre14mo ago
Virtual threads are always daemon threads. This means that running virtual threads will not prevent the application from terminating once all (non-daemon) platform threads are stopped. It is not possible to create virtual threads that are not daemon threads. There is a ThreadGroup for all virtual threads and it is not possible to create virtual thread outside of that.
📖 Sample answer from dan1st
Eric McIntyre
Eric McIntyre14mo ago
Platform threads are threads that are managed by the operating system. Creating a platform thread will instruct the OS to actually create a new system thread, which then runs your code in parallel. Virtual threads are instead managed by the JVM, and do not create a new system thread. This has the result of your code still somewhat running in parallel, but since multiple virtual threads have to be managed by one system thread, creating many virtual threads will slow the program down proportionally. Virtual threads can be useful if you want to multi thread a bunch, but don't have the required computing power to actually spawn a new system thread for every parallel task. They are, in a way, an abstraction that allows you to still write code for a parallel environment, with the added benefit of being able to swap out virtual threads for system threads on a whim.
Submission from 0x150
Eric McIntyre
Eric McIntyre14mo ago
a virtual thread is managed and scheduled by a virtual machine. a platform thread is managed and scheduled by the operating system and requires a system call to allocate or deallocate the threads.
Submission from deadpoolhawkeye
Want results from more Discord servers?
Add your server