QuickStart with OpenJDK Project Loom

Knoldus Inc.
3 min readMay 21, 2022

--

In this blog post, we will talk about Java’s ambitious project Loom which solves the problem of high throughput concurrency model using Java threads abstraction. We will go through how we can set up the minimal environment to experiment with the features of the Project Loom.

What is Project Loom

Project Loom is speculated to be the programming model which can disrupt the architecture of the “Thread per request” or “Thread per connection” by allowing concurrency much more than the actual Threads. Here is the definition borrowed from Oracle Blog.

Project Loom is an OpenJDK project that aims to enable “easy-to-use, high-throughput lightweight concurrency and new programming models on the Java platform.”

The project aims to accomplish this by adding these new constructs:

  • Virtual threads
  • Delimited continuations
  • Tail-call elimination

Where to Start? and The “Hello World!”

  • Download the pre-built binary of Loom from the OpenJDK download page.
  • Extract it to your directory named PATH
  • Switch to the PATH/bin directory and start Jshell as below:

Why --enable-preview ?

Make sure you use the switch --enable-preview. This allows you to use the experimental and Preview API in Java which is disabled by default.

What is A virtual Thread?

Till this point, we have just downloaded and run the first Hello World. Now you would be thinking how come Thread class has this static method called startVirtualThread(). This static method has been added to the Thread class in the preview version of JDK with the loom. If you would like to write the code using the preview feature, you will have to import the same version in the IDE.

How to write in an IDE

I tried writing some of the code available in Loom API by starting a new project and importing the downloaded JDK with loom in the IntelliJ and start with.

What to do Next?

As a developer, the only thing I would like to explore and understand is how performant is virtualThread when starting a lot of concurrent tasks vs starting a new Thread. So, tried the two below snippets in my jshell to observe the difference. In the first one, we are starting 100 virtual threads and in the second snippet, we are starting 100 actual Threads.

Observation on the Performance and Time!

After running the above two snippets on jshell, The one with virtual threads seems to be taking the same time to finish as the second one with Thread class. However, resources on your machine are limited and when the range will be increased, the contention can be observed in the second snippet. Now I would leave the experiment to you to do and observe how virtual threads can be created cheaply in comparison to the actual Threads. Happy hacking!!

References

knoldus

--

--

Knoldus Inc.

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com