Intro to Coroutines with Quasar

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

1. Introduction

Coroutines are an alternative to Java Threads as they provide a way to execute interruptable tasks on a very high level of concurrency, but until Project Loom is complete, we have to look to library support to get it.

In this tutorial, we'll take a look at Quasar, one such library that offers co-routine support.

2. Setup

We'll use the latest version of Quasar which requires Java 11 or higher. But, the example application will also work with prior versions of Quasar which are compatible with Java 7 and 8.

Quasar provides three dependencies which we need to include in our build:

To make use of that plugin and launch our application we will then run Maven:

mvn compile dependency:properties exec:exec

3. Implementing Coroutines

To implement the coroutine, we'll use Fibers from the Quasar library. Fibers provide lightweight threads that will be managed by the JVM instead of the operating system. Because they require very little RAM and put far less burden on the CPU, we could have millions of them in our application without having to worry about performance.

To launch a fiber, we create an instance of the Fiber<T> class which will wrap the code that we want to execute and call the start method:

Hi Amerigo,
Since Quasar works with Java 11, the latest LTS version of Java, I’d say it is a pretty useful library as of date.
However, there are alternatives to it – as the article mentions, Project Loom is one which is coming up. Then there are other libraries such as kilim and offbynull.