Javatpoint Logo
Javatpoint Logo

Java Fibers

In this article, we are going to find out what Java Fibers are and when and where they are used in the Java programming language.

What is Java Fibers?

The Java Fibers are also known as Java Virtual Machine (JVM) Fibers in the programming context. The JVM Fibers are user-mode threads that can be applied in the user mode. Every line of code that runs in Java is run in the form of threads. The abstraction and application of both the Java Fibers and Java Threads are the same. The only difference between the Java Fibers and Java Threads is that the Threads require a lot of RAM and are slower to spawn and synchronize. But the Java Fibers are completely specific to a runtime. The Java Fibers are very light weighted and are synchronized with no virtual overhead. A Java Fiber is made up of two components they are a scheduler and a continuation.

What is a Scheduler?

A Scheduler in Java is used to schedule a task to execute at a particular period of time or at certain fixed intervals of time. There are various ways of using a Scheduler in the Java programming language. The following ways are:

  1. Java library java.util.TimerTask
  2. Using the Quartz Scheduler.
  3. util.concurrent.ScheduledExecutorService
  4. springframework.scheduling.TaskScheduler

What is a Continuation?

A continuation in Java is an object which is immutable and which captures everything present in the Java stack. A continuation object in Java is used to restore the captured execution states later. A continuation capturing everything in a Java stack includes:

  1. A current instruction pointer.
  2. Return addresses.
  3. Local variables.

A Quasar Fiber

A Quasar Fiber is a Java Fiber that is used similar to a thread in the Java programming language. A Quasar Fiber in the Java implements the thread API. A Quasar will abstract both the implementations of a thread and Quasar Fiber in the Java programming language into a strand or simply a thick rope. This makes it convenient for the Fibers and Threads to co-operate without any disturbances. Let us think of a scenario where a Java application is started in the main thread. If we wish to use Fiber instead of a thread at a certain point, we can just spawn the Fiber at that particular point in the runtime and join it to the thread from that point. We could also make use of methods that work generically with strands in Java as we can use the strands as either Fibers or threads. As a matter of fact, the Quasar Fiber also includes a strand-based port of " java.util.concurrent " in Java programming language with Fiber compatibility. The Quasar approach of implementation is almost similar to the implementation used by the Kernel on OS threads. In the Quasar approach, we usually freeze the whole stack up of the Java Fiber to its basic run method, and then the method can be re-scheduled later on a commonly used Java executor. In other terms, this implementation is also called Fiber suspension.

Need for using Quasar Fibers

The operating system threads provide us with everything we need, but with a huge penalty in performance. When it comes to Fibers, they are scheduled at the application layer and can use a better scheduler which suits their use case. The major purpose of most of the Fibers is to serve transactions. So, they are active for a very short scale of time and are blocked regularly. A Fiber is Usually awakened by another Fiber as they run only for a short period of time, and they transfer the control to another Fiber which is awakened. The transfer of control between two Fibers is done by using a synchronization mechanism like a queue. The cache missing is minimal while switching Fibers in the Quasar Fibers as it uses the "work stealing" algorithm.

Are Java Fibers Generators or async/awaits?

As we have discussed, the Java Fibers are actually threads consisting of a continuation object and a scheduler. Both the generators and async/awaits are implemented with a continuation or a stackless, which can only capture a single stack frame. But these continuations don't have a scheduler associated with them. So, these Generators and async/await are not threads. Hence, they cannot be Java Fibers.


In this article, we have learned about the Java Fibers and why they are used, along with the threads in the Java programming language. We also discussed one of the major processes of implementing Java Fibers, i.e., Quasar Fibers.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA