Project Loom & Virtual Threads in Java
Tags: java, concurrency, loom, threads
Published: 2021-03-22 22:58:41

**Disclaimer**: This post will mention some terms such as concurrency, threads, multitasking, and others that will be not explained in a detailed way, but will try to add references to other posts or pages about them.

[Concurrency](https://coderstower.com/2021/02/15/java-concurrency-concurrency-and-parallelism/) in Java is  natively managed by using threads ([java.lang.Thread](https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html))  which basically is a wrapper/mapper for a native OS thread.  This model fits well in a system that does not need too many threads, however it brings some drawbacks when we want to use them in a large scale, let’s say hundreds or thousands threads.

### Why ?
* Native OS threads must support all programming languages, they are not optimised for a specific one (maybe C 🤔).
* Expensive [context switching](https://medium.com/runtimeerror/context-switching-in-depth-1d6d4e51ab32).
* High memory resource usage (stack size).

These items bring issues to escalate java applications using the thread approach to do concurrent jobs using many threads.

**E.g.** A web server application processing 500 req / sec  -> 500 threads -> 500 Mb. _Thinking about only threads created for each request, for sure a web server use other threads and memory._

Here is where Project Loom comes as a solution, so first of all let’s define what Project Loom is and what it brings to the Java world. 

**Note** Project Loom is still an ongoing project and all the information, names and definitions about it could change and there is not any official JDK released to work with. 

> **Project Loom** is to intended to explore, incubate and deliver Java VM features and APIs built on top of them for the purpose of supporting easy-to-use, high-throughput lightweight concurrency and new programming models on the Java platform - [Project Loom Wiki](https://wiki.openjdk.java.net/display/loom/Main)

Project Loom is a platform based solution introducing the concept of **Virtual Thread** (a.k.a Fiber) that is a lightweight thread managed by the Java Virtual Machine rather than the operating system and it fits with the existing Java APIs allowing synchronous (blocking) code. 

## Using Project Loom in a Java application
**Note** This post wants to show a general overview and some notions about how project  loom and virtual threads perform rather than showing any kind of micro benchmark or something deeper. 

We will use jconsole to monitor the performance and then check how many threads are created, how much memory is used and how the CPU is behaving. 

For these tests a MacBook Pro 15” 2018 will be used:
* 16Gb
* 2,2 GHz 6-Core Intel Core i7
* Mac OS Catalina

Since there is not any official release of the JDK including Project Loom, we must use the early-access binaries provided by the project. 

We can download them from the official page of the project, these binaries are based on JDK 17 and there are options for Linux, MacOS and Windows binaries. 
[Project Loom Early-Access Builds](https://jdk.java.net/loom/)

Once the binaries are downloaded and set in the PATH we can use the JDK Project Loom: 

```shell
❯ java --version
openjdk 17-loom 2021-09-14
OpenJDK Runtime Environment (build 17-loom+2-42)
OpenJDK 64-Bit Server VM (build 17-loom+2-42, mixed mode, sharing)
```

### Threads

Let’s do a naive test creating an app that creates 1000 threads and each thread run some random math operations between two numbers during a couple of minutes, and then compare the performance using the usual native threads vs virtual threads. 

```java
static class RandomNumbers implements Runnable {

    Random random = new Random();

    @Override
    public void run() {
        for (int i = 0; i < 120; i++) { // during 120 seconds aprox
            try {
                int a = random.nextInt();
                int b = random.nextInt();
                int c = a * b;
                System.out.println("c = " + c); // print to avoid compiler remove the operation
                Thread.sleep(1000); // each operation every second
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
```

Then let’s create the threads.

```java
public static void main(String[] args) throws InterruptedException {

	  // Create 1000 native threads 
    for (int i = 0; i < 1000; i++) {
        Thread t = new Thread(new RandomNumbers());
        t.start();
    }

    Thread.sleep(120000);
}
```

#### Native threads results

* Around 1000 threads.
* CPU usage between 2 and 3 %.
* Constant memory usage around 150 Mb.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/854quw0ae4s4n0ejzguc.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5wbtzlqb7nxxgqsrakpp.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m9ew3io83njpk421zgtk.png)

Now let’s do the same operation using virtual threads 

```java
public static void main(String[] args) throws InterruptedException {

	  // Create 1000 virtual threads 
    for (int i = 0; i < 1000; i++) {
        Thread.startVirtualThread(new RandomNumbers());
    }
 
    Thread.sleep(120000);
}

```

#### Virtual threads results

* Around 30 threads
* CPU usage under 2%
* Incremental memory usage from 20Mb to 60 Mb. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sjg7o5z40q6u25imrtk9.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dgj603lddopv44jp7085.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6v83uga72s68iwrepayf.png)

### Executor services

Now let’s do a test using something more elaborate using Executor service to schedule the threads .

#### CachedThreadPool Executor service

```java
public static void main(String[] args) throws InterruptedException {

    ExecutorService executor = Executors.newCachedThreadPool();

    for (int i = 0; i < 1000; i++) {
        executor.submit(new RandomNumbers());
    }

    executor.shutdown();

    if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
        executor.shutdownNow();
    }
}
```

This implementation of executor service uses a new thread (native) per task scheduled unless there is a free thread to take it. 

These results look pretty similar to the threads approach

* Around 1000 threads.
* CPU usage around 2 %.
* Constant memory usage around 130 Mb.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k09foymzwufbk1gmniba.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgh29cxywc07bdxczuev.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sjqjepecjij76fdhkag5.png)

#### VirtualThreadExecutor Executor service

```java
public static void main(String[] args) throws InterruptedException {

    ExecutorService executor = Executors.newVirtualThreadExecutor();

    for (int i = 0; i < 1000; i++) {
        executor.submit(new RandomNumbers());
    }

    executor.shutdown();

    if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
        executor.shutdownNow();
    }
}
```

These results show a light decrease of memory and CPU usage. 
* Around 30 threads.
* CPU usage between 1 and 2 %.
* Incremental memory usage from 30Mb up to 40 Mb. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u93e760quf37bsz1ebbo.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7p9in3mzwntg2s00eh5v.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ejr92kvv1a68oc58dgx.png)

## Conclusion
We can see how the use of virtual threads could help us when we need to use concurrency using thousands of threads without compromising performance and making optimal use of resources. 

Project Loom is promising a good future to Java and its concurrency API to be at the level of other languages that already have lightweight concurrency models. 

_If you like this post you can find more in https://jeisson.dev/blog/ and follow me in twitter @jeissonflorez29 👋

[OpenJDK: Loom](https://openjdk.java.net/projects/loom/)