Java's Project Loom

— 2 minute read

An upcoming Java feature I'm excited about and looks like a game-changer is Project Loom.

It's an improvement to the concurrency model which introduces lightweight virtual threads (aka fibers, aka user-mode threads) into the Thread API. These will exist alongside the existing platform threads (aka kernel threads).

The upshot is that Java can manage your threaded programs more efficiently and allow you to write simpler concurrent code. The majority of the work is done under the hood with very minimal API changes.

For example, where a developer would have to consider sizing and managing thread pools, Loom allows you to simply use a thread per task. Java, instead of the OS, will take care of scheduling these virtual threads and mapping them to platform threads. Benefiting from this may be as easy as replacing:

Executors.newFixedThreadPool(NTHREADS);

with this:

Executors.newVirtualThreadExecutor();

For highly-concurrent applications ― high-traffic web servers say ― the current limitations of expensive platform threads can warrant a major design choice to use an asynchronous paradigm, such as Reactive. Loom proponents contest that this is now unnecessary because you can instead create millions of virtual threads and write the same straightforward code that would previously have only served thousands of requests.

I recently attended a Manchester Java Community Q&A event with the Project Loom lead, Ron Pressler ― a highly recommended watch...