DEV Community

loading...
Cover image for Java threads are cheap! Let’s create millions of them

Java threads are cheap! Let’s create millions of them

bazlur_rahman profile image MasterDevSkills ・3 min read

Photo by Aditya Wardhana on Unsplash

Java threads are cheap. Yes, you heard me, right. With the feign of the project loom, you can literally create millions of threads in java. Let’s talk about it.

Disclaimer: in this article, I will not explain how thread and virtual thread works and the underlying concept; rather, I will demo that we can create millions of virtual threads using project loom.

Threads are everywhere in java. For example- in most java web applications, we assign a thread to deal with a pair of requests/responses. Each request/response are independent. On a request, the server parses the request, if needed, query over the database and sends a response. It simple and easy. It doesn’t matter how many of them are being served; the programming model is the same and simple.

Nowadays, a web server can handle millions of concurrent open sockets. That means if we can create more threads, our application would be able to handle more requests concurrently. This conclusion may sound intuitive, but in reality, we can only create a limited number of threads. The operating system cannot efficiently handle more than a few thousand active (non-idle) threads.

Let’s do an experiment and figure out many threads we can create in a java application.

package com.bazlur;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

public class ThreadCreationDemo {
 public static void main(String[] args) {
  var counter = new AtomicInteger();
  while (true) {
   new Thread(() -> {
    var count = counter.incrementAndGet();
    System.out.println("count = " + count);
    LockSupport.park();
   }).start();
  }
 }
}
Enter fullscreen mode Exit fullscreen mode

Using a while loop, we will keep creating a new thread and then park it. Using the AtomicInteger, we will count how many threads we are able to create and let the application crash.

Using my computer*, I was able to create 4058 threads, and then the application crashed.

As you can see, we are limited in terms of how many threads we can create and be able to use in an application.

Lately, a new project is being developed called project loom, which will solve this limitation. Using it, we can literally create millions of virtual threads.

I will do a similar experience using project loom over here now-

package com.bazlur;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

public class VirtualThreadCreationDemo {
 public static void main(String[] args) {
  var counter = new AtomicInteger();

  while (true) {
   Thread.builder().virtual().task(() -> {
    var threadCount = counter.incrementAndGet();
    System.out.println("count = " + threadCount);
    LockSupport.park();
   });
  }
 }
}
Enter fullscreen mode Exit fullscreen mode

Using the above code, I was able to create more than 4 million virtual threads.

The key differences between threads and virtual threads are -

  • Virtual threads are just about the regular thread - in code, runtime, and the dubber and the profile. That means developers don’t need to learn new stuff.
  • A virtual thread is not a wrapper around an OS thread but a java entity.
  • Virtual thread is super cheap.

If you want to learn more about project loom, here are the resources-

https://cr.openjdk.java.net/~rpressler/loom/loom/sol1_part1.html
https://wiki.openjdk.java.net/display/loom/Main

Computer configuration: MacBook Pro (15-inch, 2019), 2.3 GHz 8-Core Intel Core i9, 16 GB Memory.

This post originally posted in Java threads are cheap! Let’s create millions of them | LinkedIn

Discussion (0)

pic
Editor guide