A quick introduction to Virtual Thread(Loom).

Reading Time: 2 minutes

Virtual Thread makes the java program to be asynchronous. However, It has the feature of the synchronous programming. Why this is needed you can go through my previous blog. In this blog, I am going to explain how java achieves this through Loom.

Virtual thread is the combination of continuation and scheduler. Let’s dive in to understand these two concepts.

Continuation

The Continuation can be also seen as a java object which consists of the virtual thread state. As we are writing the asynchronous code-switching between the code is officious. Switching with virtual thread how it happens is through the continuation. It is a java class that consists of some data fields which is going to store the state of the java code where it left and form where it going to start the execution.

A continuation is a sequential sub-program with an entry point (like a thread), entry point, which may suspend or yield execution at some point, which we’ll call the suspension point or the yield point. When a continuation suspends, control is passed outside of the continuation, and when it is resumed, control returns to the last yield point, with the execution context up to the entry point intact. The following rough pseudocode would explain it a little bit of continuation.

method1() { // (step 3)
  method2()
} 
method2() {
  suspend // (step 4)
  ... // (step 6)
}
​main() {
  c = continuation(method1) // (step 1)
  c.continue() // (step 2)
  c.continue() // (step 5)
}

The main method had made a continuation object at step 1. At step 2 we have called the continuation. Method1 (step 3) is called, from there we have called the method2. At step4 we have suspended the execution of the thread. If the yield is called from anywhere from the code, control directly goes to the caller of the method. Therefore we went to step 5. It is continuation step that leads to the resume of the thread and control go back to step 6 after the yield line of method2.

How information is stored for continuation?

When a continuation got suspend. Continuation object store the information where it left the execution in the heap. It will maintain the two stack one will store the primitive data and in second it will store the reference of the objects. Initial it will store the complete stack trace and then if continuation is called again it will do the lazy copying. In which it will only maintain the update or changed trace.

Scheduler

As we switching between the thread or say virtual thread. Which virtual thread going to execute which has to stop. We need a scheduler by default we use the fork and join scheduler.

This how java or say Loom implemented the virtual thread. The Virtual thread which the combination of continuation and scheduler.

That’s all for the small introduction of How a virtual thread is implemented? If you have any queries or want to know more about it you can add the comments below. I am happy to answer them. 🙂

References

Knoldus-blog-footer-image

Written by 

Priyanka Thakur is a Software Consultant having 6 months of experience currently working at Knoldus Software LLP. She has done MCA at BVICAM, GGSIPU. She has a graduation degree in BCA from JIMS, GGSIPU. She is familiar with programming language' s such as C, Java, and Scala. She also has interest in Angular and Docker and currently working on Logam. She is dedicated, focused and hardworking person. Her interests are in to face new programming challenges because she believes these challenges produce opportunity. She is keen to learn new technologies. In her leisure time, she prefers reading about mythology, watching movies.

Leave a Reply