Learn from Java Champion A N M Bazlur Rahman: Let’s Use Virtual Threads and Then Forget About Them

Learn from Java Champion A N M Bazlur Rahman: Let’s Use Virtual Threads and Then Forget About Them

October 31, 2022

In our new Java Monthly edition, we’d like to introduce you to A N M Bazlur Rahman. He was kind enough to share his experience on 9 more Java-related questions.

A N M Bazlur Rahman is a Software Engineer, Java Champion, Author, Blogger, and Speaker. He has more than a decade of experience in the software industry, primarily with Java and Java-related technologies.

He enjoys mentoring, writing, delivering talks at conferences, and contributing to open-source projects. He is the founder and current moderator of the Bangladesh Java User Group. In addition, he is an editor for the Java Queue at InfoQ and




Dreamix: What are the changes to Java multithreading from Java 8 to Java 18 and future Java 19 that are important and worth mentioning?

A N M Bazlur Rahman: There are a few changes in concurrency from Java 8 to Java 18. A few JEPs are focused on internal changes that improve performance. For example, a new method named “onSpinWait” was added in the Thread class as part of JEP 285 to allow Java code to hint to the CPU that there is a busy-waiting loop that may burn a few CPU cycles waiting for something to happen. Besides this, a few other JEPs,  JEP 143: Improve Contended Locking, JEP 312: Thread-Local Handshakes, and JEP 270: Reserved Stack Areas for Critical Sections, were delivered. Those who are interested can look into those JEPs, but the major changes are added in JDK 19 under the umbrella of Project Loom. In JDK 19, a new concept called “virtual threads” is being added as a preview feature, aiming to dramatically reduce the effort of writing, maintaining, and observing high-throughput concurrent applications on the Java platform. This is under JEP 425. If your readers are interested, they can look at this news item I published on InfoQ. 

Dreamix: What is Structured Concurrency’s (Incubator) effect on performance? Can you give us any numbers if it is measured?

A N M Bazlur Rahman: Well, this is still actively being developed. Performance isn’t really a focus at this moment, I believe. Secondly, structured concurrency is going to work on top of Virtual Threads. When a virtual thread hits a blocking operation, it is put on hold until it can be picked up again later. When it is yielded, it just sits in the memory but doesn’t consume the CPU. For that reason, I don’t see much reason to worry about performance. I hope that once the API is stable, it will be delivered as a preview feature; as a developer, we will all be able to test it out, and then, of course, we will be able to share our feedback. 

Although it is an incubator feature, we can still try it out with JDK 19. If you have JDK 19, you can use it using the following commands – 

javac –release 19 –enable-preview –add-modules

The same flag is also required to run the program:

java –enable-preview –add-modules JDK.incubator.concurrent Main;

The “Main” here is the class where you can write the code. E.g. 

Response handle() throws ExecutionException, InterruptedException {
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
Future<String> user = scope.fork(() -> findUser());
Future<Integer> order = scope.fork(() -> fetchOrder());

       scope.join();          // Join both forks
scope.throwIfFailed(); // … and propagate errors

       // Here, both forks have succeeded, so compose their results
return new Response(user.resultNow(), order.resultNow());

To know more about it, you can read my news item on InfoQ:

Dreamix: Is structured concurrency supporting both normal threads and virtual threads, as mentioned in JEP 425

A N M Bazlur Rahman: No, structured concurrency is going to work on top of virtual threads only.

Dreamix: In one of your articles you mentioned that you can create 4.5 million threads with Project Loom. With this new way of creating threads, will there be any changes to how we handle threads and their performance? What should be our new considerations when creating a thread?

A N M Bazlur Rahman: The idea of virtual threads is like creating a new object that will perform a task. This is pretty much the way we submit tasks to the scheduler. You will basically create your task and hand it over to the virtual thread and then forget about it. It’s the same way we create a Java object and then forget about it. 

Dreamix: When should we create virtual threads, and when should we create conventional threads?

A N M Bazlur Rahman: When there is a blocking operation, e.g., a network call, we can use virtual threads. The virtual thread is solving one problem, and it is doing well, which is blocking operations.  But they are not suitable for CPU-intensive work. For CPU-intensive work, we can just use the traditional threads. Virtual Threads now allow us to write high-throughput servers easily.  

Dreamix: What are the future goals and milestones for Project Loom? Does the project have any intent on touching the concept that is the nightmare of all concurrent programs; Deadlocks?

A N M Bazlur Rahman: One such goal is tail-call elimination. But I haven’t seen any targeted JEP for that yet. The idea is that, as we all know, the stack size in Java is limited, so if we keep building a stack, at some point, we will no longer be able to allocate more as memory runs out, and thus we usually get a StackOverflowException. The idea is that the virtual threads use continuation. The continuation is an API by which a piece of code can be paused at a certain time from executing and then resumed precisely from there. More about the continuation can be found here: The idea of tall-call elimination is that, when using continuation, the system will be able to avoid allocating new stacks whenever possible. Since there is no JEP for this, it is too early to comment on it, but I am hopeful that something good will come out of it in the end.

And about the second part of the question—to be able to use virtual threads, we don’t really have to learn a lot of new concepts. In fact, all the APIs are the same as if there was nothing new for the developers to use. So the concept of using threads remains the same. And so, the concept of DeadLock remains the same. Nothing in the project loom that I know of has anything new to help with it.

Dreamix: Are there any future plans, changes or additions to the conventional locking mechanisms (ReentrantReadWriteLock etc.) or semaphores with the arrival of Structured Concurrency?

A N M Bazlur Rahman: Not that I know of as of now. But the internal API, especially thread-safe classes, e.g. ConcurrentHashMap, AtomicInteger, etc., was already written in a lock-free way many years ago, so that’s an alternative option. You can definitely look into the implementation of those classes if you are interested. Here is a talk that might interest you : Scale Up with Lock Free Algorithms

But I wouldn’t recommend it for the regular developer to go that route and implement their own version for our regular day-to-day use cases, but instead use the classes that are available in the JDK, as it is easy to make mistakes. 

Dreamix: How do you update yourself about the latest trends in Java?

A N M Bazlur Rahman: Twitter can be an excellent way to learn about trends. I usually follow a group of people on Twitter who I look up to and see what they are discussing. The other option is the mailing list. You can follow a number of them form here:

You could also go to conferences, attend sessions, or join a local Java User Group. There are also some awesome content platforms, e.g., InfoQ,, etc. You can read their content. Finally, reading books that have come out lately is a great option to learn and learn new stuff. 

Dreamix: Can you recommend a favourite book about programming? What about a favourite book in general?

A N M Bazlur Rahman: There are many, in fact. But I can tell you what I am reading now: 

  • The Well-Grounded Java Developers (2ne edition) by Benjamin J. Evans. 

Apart from that one, I think Effective Java by Joshua Bloch and Java Concurrency in Practices by Brian Goetz are two must-reads for every Java developer. 

Is there anything else you would like to ask A N M Bazlur Rahman? What is your opinion on the questions asked? Who would you like to see featured next? Let’s give back to the Java community together!

Dreamix Team

Innovators by heart. Developers by passion. We’re Dreamix Team - a group of trailblazing techies trying to make the world a better place through technology. We provide custom software development, keep you updated on market and industry trends, and have a great time doing it.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInPinterestGoogle Plus

Do you want more great blogs like this?

Subscribe for Dreamix Blog now!