The main goal of Project Loom is to support an easy-to-use, a high-throughput, lightweight concurrency model in Java platform.
Limitation of using Multi-threading application:
CPU switches from executing one thread to executing another to allow multiple processes to share a single CPU. The CPU needs to store the state the current process or thread or update the next thread to execute. This switch is called Context switch. The context switch is expensive and it requires a certain amount of time for doing the administration – saving and loading registers and memory maps, updating various tables and lists, etc
That's why Fibers were introduced in the project Loom. So before we get to fibers, we will explain continuation which is the basic construct that fiber is build on top of it.
The main aim of continuation is the implementation of Fibers. Continuations is sequential code that may suspend (itself) and resume (be resumed by a caller).
Following is an example of Continuation using jshell.
Figure 2: Example of creation and use of a Continuation object
To create a continustion, a scope is needed (ContinuationScope). Continuation runs and yields occasionally and then outside the continuation, while the continuation is not done, you run it and each time will continue from the last place where it yielded until it terminates.
It is a light weight or user-mode thread, scheduled by the java virtual machine; and not the operating system. The most important thing is that they have negligible task-switching overhead.
Fibers are made of two components: Continuation and scheduler.
Fibers wrap any task in an internal user-mode continuation. This would allow the task to suspend and resume in Java runtime instead of the kernel.
The scheduler in the form of
Following is an exmpale of how to create a Fiber. To download and build the loom project, follow the instruction in this link https://wiki.openjdk.java.net/display/loom/Main.
Figure 1: Example of creation of a Fiber object
Note: JShell is a Java read-eval-print loop tool first introduced in the JDK9.
Ron Pressler , Alan Bateman. Project Loom. https://www.youtube.com/watch?v=J31o0ZMQEnI
Mark Reinhold . Java, Today and Tomorrow. https://www.youtube.com/watch?v=Csc2JRs6470
Openjdk. Project Loom: Fibers and Continuations for the Java Virtual Machine.https://cr.openjdk.java.net/~rpressler/loom/Loom-Proposal.html