Project Loom: Understand the new Java concurrency model

It helped me think of virtual threads as tasks, that will eventually run on a real thread⟨™) (called carrier thread) AND that need the underlying native calls to do the heavy non-blocking lifting. On my machine, the process hung after 14_625_956 virtual threads but didn’t crash, and as memory became available, it kept going slowly. It’s due to the parked virtual threads being garbage collected, and the JVM is able to create more virtual threads and assign them to the underlying platform thread.

One of the main goals of Project Loom is to actually rewrite all the standard APIs. For example, socket API, or file API, or lock APIs, so lock support, semaphores, CountDownLatches. All of these APIs need to be rewritten so that they play well with Project Loom. However, there’s a whole bunch of APIs, most importantly, the file API. There’s a list of APIs that do not play well with Project Loom, so it’s easy to shoot yourself in the foot.

Portable Language Runtimes as Java Libraries

This includes built-in flow control (if, while, for), as well as custom flow-control, defined using higher-order functions (e.g. forEach, map, filter). If we are running in a fiber, we can safely call “blocking” (fiber-suspending) code. «Leveraging that model, you could build apps that, in terms of utilizing resources, are on par with an asynchronous or reactive programming model,» he said. «It’s interesting to see these competing models, and in general just getting improvements in the existing system.»

  • Let’s look at some examples that show the power of virtual threads.
  • Servlet asynchronous I/O is often used to access some external service where there is an appreciable delay on the response.
  • This is a main function that calls foo, then foo calls bar.
  • The structured concurrency API is also designed to preserve order in multi-threaded environments by treating multiple tasks running in individual threads as a single logical unit of work.
  • If you are doing the actual debugging, so you want to step over your code, you want to see, what are the variables?

There is no extra level of complexity that arises from the fact that a large number of actors has to share a small number of threads. This is a user thread, but there’s also the concept of a kernel thread. A kernel thread is something that is actually scheduled by your operating project loom java system. I will stick to Linux, because that’s probably what you use in production. For example, when a kernel thread runs for too long, it will be preempted so that other threads can take over. It more or less voluntarily can give up the CPU and other threads may use that CPU.

Loom and the future of Java

Moreover, code that catches all exceptions is not uncommon. In this case, care must be taken to re-throw cancellation exceptions. That’s one of many examples that show why exceptions aren’t a good tool for control flow. The operators that we might want to add can cover error handling, threadpool-pinning, repeated evaluation, caching, and most importantly, safe resource allocation. Taking these ideas one step further, we have the actor model (known from Erlang and Akka), in which isolated processes communicate solely by message-passing. Asynchronous, non-blocking APIs also play a big role here; they allow scheduling of sending messages to actors, when a given resource is available, or when an I/O call completes.

project loom java

Structured concurrency will be an incubator feature in Java 19.

Thread Sleep

Explore the possibility to hire a dedicated R&D team that helps your company to scale product development. But before we dive into the intricacies of Project Loom, let’s first understand the broader context of concurrency in Java. Discover new ideas and insights from senior practitioners driving change in software.

project loom java

Each time Java does blocking I/O, waits on a lock/mutex or sleeps, this can now take advantage of the new mechanism. A blocking I/O call executed on a fiber will now use non-blocking I/O under the hood. The execution of a fiber can be suspended at explicitly defined yield points. When a fiber yields, other fibers might be executed by the scheduler.

What does this mean to regular Java developers?

In our case, I just create an executor with just one thread. Even with just a single thread, single carriers, or single kernel thread, you can run millions of threads as long as they don’t consume the CPU all the time. Because, after all, Project Loom will not magically scale your CPU so that it can perform more work. It’s just a different API, it’s just a different way of defining tasks that for most of the time are not doing much.

It’s tempting to treat everything as a synchronous call; but sometimes you have to resist the temptation. Still, we can use nicer syntax for asynchronous calls, using an async/await like construct. The CPS transform and hence Kotlin’s coroutines also have other possibilities, such as defining generators or stream processors. Another example are async/await constructs, which help to mitigate the lost control flow problem. These are now native in Javascript, available as a language feature in C# and Kotlin, and as a library feature in Scala. What seems to be the hardest part, is retrofitting existing blocking Java APIs so that they become fiber-aware.

Может ли Java быть быстрой? Сравнение производительности Helidon Níma и Spring

You essentially say Thread.startVirtualThread, as opposed to new thread or starting a platform thread. A platform thread is your old typical user threads, that’s actually a kernel thread, but we’re talking about virtual threads here. Typically, ExecutorService has a pool of threads that can be reused in case of new VirtualThreadExecutor, it creates a new virtual thread every time you submit a task. You can also create a ThreadFactory if you need it in some API, but this ThreadFactory just creates virtual threads.

The project developers also plan to experiment with new schedulers and continuations. But mainly, with the last component as the scheduler can be easily used with Fiber. The new thread, Fiber, consists of just two components — a continuation and a scheduler.

The Seven Hard Truths About Software Development Career

The second of these stages is commonly the last development phase before incorporation as a standard under OpenJDK. Of course, there are some limits here, because we still have a limited amount of memory and CPU. Now it’s easy, every time a new HTTP connection comes in, you just create a new virtual thread, as if nothing happens. This is how we were taught Java 20 years ago, then we realized it’s a poor practice. These days, it may actually be a valuable approach again.

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Scroll al inicio