

A quick introduction to Virtual Thread(Loom).
source link: https://blog.knoldus.com/a-quick-introduction-to-virtual-threadloom/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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
Recommend
-
72
Project Loom: Fibers and Continuations for the Java Virtual MachineOverviewProject Loom's mission is to make it easier to write, debug, profile and maintain concurrent applications meeting today's requirements. Threads, provided...
-
170
-
54
Recently, I’ve been working on a Java application that suffered some serious performance issues. Among many problems, the one that really grabbed my attention was a relatively slow allocation rate of new objects (the appl...
-
52
Early Access Loom Builds Early Access Loom Builds Ron Pressler
-
8
Loom and Thread Fairness Posted at May 27, 2022 Project Loom (JEP 425) is probably amongst the most awaited feature addition...
-
6
Java Concurrency: An Introduction to Project Loom Poste...
-
9
#Java
-
3
Pitfalls to avoid when switching to Virtual threads
-
6
In a previous post, we have seen how to implement a CRUD application using virtual threads in Quarkus. The following video shows how to test this application and, speci...
-
10
Blog Containerizing virtual thread applications Containerizing virtual thread applications
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK