Virtual Threads
Haim Michael
July 11th
, 2023
All logos, trade marks and brand names used in this presentation belong
to the respective owners.
life
michae
l
Java
in
© 2008 Haim Michael 20230307
What is Virtual Thread
 The virtual thread is a thread without a native thread running
on the operating system level.
© 2008 Haim Michael 20230307
Simple Demo
Thread t1 = Thread.ofVirtual().name("moshico1").start(() -> {
for(int i=0; i<100; i++) {
System.out.println("i="+i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
© 2008 Haim Michael 20230307
The Stack
 The virtual thread stores the stack frames in the heap
memory. Thanks to that the memory footprint of virtual
threads is relatively small.
© 2008 Haim Michael 20230307
Executor Services
 We can use a new implementation of the
java.util.concurrent.ExecutorService tailored for
virtual threads. Its name is ThreadPerTaskExecutor.
 This executor creates a new virtual thread for every task we
submit.
var executor = Executors.newVirtualThreadPerTaskExecutor();
© 2008 Haim Michael 20230307
How Virtual Thread Work?
 The JVM maintains a pool of platform threads. Initially the
number of the platform threads equals the number of the CPU
cores.
 Whenever a virtual thread is created, the JVM schedules its
execution on one of the platform threads, and copies the
stack of the virtual thread from the heap to the stack of the
platform thread (AKA carrier thread).
© 2008 Haim Michael 20230307
How Virtual Thread Work?
 When a virtual thread is stuck on a blocking operation the
carrier thread is released and the stack chunk of the virtual
thread is copied back to the heap.
 When the blocked virtual thread finishes the blocking
operation, the scheduler schedules it again for execution on
the same carrier thread or another one.
© 2008 Haim Michael 20230307
Pinned Virtual Threads
 There are few cases where a blocking operation doesn’t
unmount the virtual thread from the carrier thread. As a result,
the carrier thread is blocked.
 In these cases we will describe the virtual thread as pinned to
the carrier thread.
 There are two cases in which a virtual thread is pinned to the
carrier thread: When it executes code inside a synchronized
block and when it calls a native method.
© 2008 Haim Michael 20230307
Questions & Answers
Thanks for Your Time!
Haim Michael
haim.michael@lifemichael.com
+972+3+3726013 ext:700
life
michae
l

Virtual Threads in Java

  • 1.
    Virtual Threads Haim Michael July11th , 2023 All logos, trade marks and brand names used in this presentation belong to the respective owners. life michae l Java in
  • 2.
    © 2008 HaimMichael 20230307 What is Virtual Thread  The virtual thread is a thread without a native thread running on the operating system level.
  • 3.
    © 2008 HaimMichael 20230307 Simple Demo Thread t1 = Thread.ofVirtual().name("moshico1").start(() -> { for(int i=0; i<100; i++) { System.out.println("i="+i); try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } } });
  • 4.
    © 2008 HaimMichael 20230307 The Stack  The virtual thread stores the stack frames in the heap memory. Thanks to that the memory footprint of virtual threads is relatively small.
  • 5.
    © 2008 HaimMichael 20230307 Executor Services  We can use a new implementation of the java.util.concurrent.ExecutorService tailored for virtual threads. Its name is ThreadPerTaskExecutor.  This executor creates a new virtual thread for every task we submit. var executor = Executors.newVirtualThreadPerTaskExecutor();
  • 6.
    © 2008 HaimMichael 20230307 How Virtual Thread Work?  The JVM maintains a pool of platform threads. Initially the number of the platform threads equals the number of the CPU cores.  Whenever a virtual thread is created, the JVM schedules its execution on one of the platform threads, and copies the stack of the virtual thread from the heap to the stack of the platform thread (AKA carrier thread).
  • 7.
    © 2008 HaimMichael 20230307 How Virtual Thread Work?  When a virtual thread is stuck on a blocking operation the carrier thread is released and the stack chunk of the virtual thread is copied back to the heap.  When the blocked virtual thread finishes the blocking operation, the scheduler schedules it again for execution on the same carrier thread or another one.
  • 8.
    © 2008 HaimMichael 20230307 Pinned Virtual Threads  There are few cases where a blocking operation doesn’t unmount the virtual thread from the carrier thread. As a result, the carrier thread is blocked.  In these cases we will describe the virtual thread as pinned to the carrier thread.  There are two cases in which a virtual thread is pinned to the carrier thread: When it executes code inside a synchronized block and when it calls a native method.
  • 9.
    © 2008 HaimMichael 20230307 Questions & Answers Thanks for Your Time! Haim Michael haim.michael@lifemichael.com +972+3+3726013 ext:700 life michae l