Java 19 Project loom - Java's answer to Go and Kotlin's coroutine?
Java 19 is slated to be released on Sep 20th 2022 and so does the new feature that excites us. In my last article https://www.dhirubhai.net/feed/update/urn:li:activity:6972823482699059200/ I wrote about using Spring Web flux to use Reactive programming style communication with server's like Netty, Undertow etc., that leverages event loop style request/responses.
Now this article focusses on writing better concurrency programming in Java using Threads but without burdening the CPU usage like normal Java Thread which we have been using for a long time now. Go and Kotlin differentiated themselves by creating coroutines which allowed developers to write concurrency code very easily. This also forced many of the developers to learn and use these languages since we did not have any alternatives in Java. Go and Kotlin have other advantages apart from coroutines, but this article was just looking at the concurrency coding part.
Now comes Virtual Thread in Java 19 which is part of Project Loom. Although the code for this project started way back around 2017, the feature is getting released in a preview mode as part of Java 19. In simple terms, Virtual Thread as apposed to normal Java Thread, does not use the OS Thread, instead, creates a logical thread on each OS thread in JVM. Hence for each virtual thread, it does not need a new OS thread. And guess what, these are going to be short lived threads and they close themselves immediately once completing the task, just like coroutines in Go and Kotlin. The good news is, creating these virtual threads are as simple as creating normal Threads since they are new methods added to existing Thread class or Executors. Look at the code below to see how similar it is, to create virtual thread, compared to normal Thread.
Normal Thread creation,
领英推荐
try (var executor = Executors.newFixedThreadPool(10))
? for(int i = 0; i < 1000; i++) {
? ? executor.submit(runnable);
? }
}{
Virtual Thread creation,
try (var executor = Executors.newVirtualThreadPerTaskExecutor())
? for(int i = 0; i < 1000; i++) {
? ? executor.submit(runnable);
? }
}{
It is that simple. This makes a huge difference. Normal thread for the same code would create multiple parallel threads, which is basically part of OS Thread that are highly resource intensive and has the limitation of CPU and the number of cores in the machine. E.g., an 8 core CPU has 16 threads max (8 Physical and 8 virtual). So in a normal thread code, JVM has to play around with maximum of 16 threads in a 8 CPU machine. But now, with Virtual thread creation, you can almost have limitless amount of virtual threads created without burdening the CPU since they are short lived threads which are spawn out of utilizing existing OS threads, and are closed and released within a negligible amount of time.
I think, this is a big win for Java developers who are thinking whether to switch to Go or Kotlin for the sake of concurrency programming. It is a also a good option for a team, which does not have Go or Kotlin programmers, but need a similar kind of coding and performance. It is a delayed feature in Java 19, but I think, it is a much welcoming one. Lets start experimenting and using this.