This content originally appeared on Level Up Coding - Medium and was authored by Arpit Jindal
![](https://cdn-images-1.medium.com/max/600/0*G_lzqs7aq5X0YLCq.jpg)
If you are new to virtual threads, check out this article for a basic understanding of Project Loom and virtual threads:
Project Loom and Virtual Threads in Java
Continuation is a programming technique that allows a program to pause its execution at a specific point and later resume execution from where it left off. In Java, continuation is implemented using the Continuation API, which is part of the Coroutine Framework. The Continuation API provides a way for a program to capture its current state, including its call stack and local variables, and later restore that state to resume execution.
Continuations are useful for implementing cooperative multitasking, which is a programming model where tasks voluntarily give up control to other tasks to allow them to execute. In cooperative multitasking, tasks are designed to cooperate with each other by voluntarily giving up control to allow other tasks to execute, rather than being interrupted by the operating system.
Virtual Threads and Continuation
In Java 19, a new feature called "virtual threads" was introduced as a preview feature to provide lightweight concurrency. Virtual threads are a type of thread that can be created and managed by the Java Virtual Machine (JVM) itself rather than being created and managed by the operating system. Because virtual threads are lightweight, they can be created and destroyed more quickly than traditional threads, which can improve the performance of applications that require a large number of concurrent threads.
Virtual threads are implemented using Continuations. When a virtual thread is created, a new Continuation is created to represent the thread’s state. The Continuation captures the current state of the thread, including its call stack and local variables, and allows the thread to later resume execution from where it left off.
How it works
When a virtual thread is created, it is associated with a Continuation object that captures its current state. The virtual thread can then perform some computation or I/O operation, and if it needs to be suspended, its Continuation object is saved to the heap, and its state is restored to a previous point in time. When the I/O operation completes, the virtual thread can resume execution from where it left off by restoring its Continuation object from the heap and returning to the suspension point.
![](https://cdn-images-1.medium.com/max/608/1*gZrqY2US-q3D1fv9nq4XIA.png)
Conclusion
In Java, virtual threads are implemented using continuations, which allows for lightweight, efficient concurrency. By using virtual threads and continuations, Java developers can write concurrent applications that are easy to reason about and performant, without the overhead and complexity of traditional thread-based concurrency.
References
- https://www.infoq.com/presentations/continuations-java/
- https://www.youtube.com/watch?v=eU9mUmnhjb4&ab_channel=Devoxx
Continuation and Virtual Threads in Java: A New Concurrency Paradigm was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Arpit Jindal
![](https://www.radiofree.org/wp-content/plugins/print-app/icon.jpg)
Arpit Jindal | Sciencx (2023-04-25T12:57:51+00:00) Continuation and Virtual Threads in Java: A New Concurrency Paradigm. Retrieved from https://www.scien.cx/2023/04/25/continuation-and-virtual-threads-in-java-a-new-concurrency-paradigm/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.