#Java 19 introduces virtual threads to the Java platform for the first time; this is the primary deliverable of OpenJDK's Project Loom.

This thread is all about it:
🧵

⬇️⬇️⬇️
Java is made of threads. When we run a Java program, its main method is invoked as the first call frame of the main thread created by the Java launcher. It gives us many things: sequential control flow, local variables, exception handling, single-step debugging, and profiling. ⬇️
It makes our lives easier by providing exception handling with informative stack traces and serviceability tools that let us observe what's happening in each thread, providing remote debugging, and creating an illusion of sequentiality that makes our code easier to reason.
⬇️
Most JVM implementations today implement Java threads as thin wrappers around operating system threads.
We call these heavyweight, OS-managed threads platform threads. 
⬇️
Operating systems typically allocate thread stacks as monolithic memory blocks at thread creation time that cannot be resized later—generally 2 MB (on Linux).
One million threads would require 2 terabytes of memory!
It essentially means that we can't have a lot of them.
⬇️
In a server application, a thread is assigned to each incoming request. This approach scales well for moderate-scale applications, e.g., 1000 concurrent requests, but cannot survive 1M concurrent requests, even though we have adequate CPU capacity and IO bandwidth.
⬇️
Virtual threads are an alternative implementation of Java threads that store their stack frames in Java's garbage-collected heap rather than in monolithic blocks of memory allocated by the OS. It starts out at only a few hundred bytes and expands and shrinks automatically.
⬇️
The operating system only knows about platform threads, which remain in the scheduling unit. To run code in a virtual thread, the Java runtime arranges for it to run by mounting it on some traditional thread called a "carrier thread."
⬇️
When code running in a virtual thread would otherwise block for IO, locking, or other resource availability, it can be unmounted from the carrier thread, and any modified stack frames copied back to the heap, which frees the carrier thread to run something else.
⬇️
With this virtual thread, we get all the benefits traditional threads have, plus it is cheap, lightweight, and virtually free. Moreover, we can create many of them.
The following example creates 100,000 virtual threads in under 2 seconds.  
⬇️ Image
Since a large number of virtual threads are easy to create, the thread-per-request programming style alleviates this scalability bottleneck.
In other words, high throughput is very simple to achieve.
⬇️
Since this is a preview feature, a developer will need to provide the --enable-preview flag to compile this code, as shown in the following command:

javac --release 19 --enable-preview Main.java
⬇️
The same flag is also required to run the program:

java --enable-preview Main

However, one can directly run this using the source code launcher. In that case, the command line would be:

java --source 19 --enable-preview Main.java
⬇️
While Thread.startVirtualThread(Runnable) is the convenient way to create a virtual thread, new APIs like Thread.Builder, Thread.ofVirtual(), and Thread.ofPlatform() were added to create virtual and platform threads.
⬇️
Last but not least, if you liked this thread and want to read more like it in the future, follow me on Twitter (why wouldn't you?).

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with A N M Bazlur Rahman 🇧🇩🇨🇦

A N M Bazlur Rahman 🇧🇩🇨🇦 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @bazlur_rahman

Sep 19
#Java 19 is a major release, IMO. It includes several game-changing features that will alter the Java landscape. Many features intrigue my interest, but there are five in particular that I can't wait to try out. Let's break those down and talk about them separately.
🧵
⬇️⬇️⬇️
Under the umbrella of Project Loom, JEP 425 introduces virtual threads, which aim to dramatically reduce the effort of writing, maintaining, and observing high-throughput concurrent applications on the Java platform. ⬇️
Structured Concurrency allows you to treat multiple tasks running on different threads as an atomic operation, making multithreaded programming easier. As a result, error handling and cancellation will be simplified, reliability will increase, and observability will be boosted.⬇️
Read 7 tweets
Jun 26
This Twitter thread is about @JakartaEE 10.

Quick recap: #JavaEE was renamed #Jakarta EE; it transitioned from JCP to Eclipse foundation. It opened the door to open governess, open compatibly testing (open TCK), and of course, open-source.

Follow the threads 🧵👇👇👇
@JakartaEE is an integral part of the Java ecosystem. 25-35% of java application runs on JakartaEE application servers, e.g. #WildFly, #JBoss EAP, #Payara, #WebSphere/#Liberty, #WebLogic etc.

👇
70-80% of java applications use JakartaEE APIs one way or another. e.g. Tomcat, Hibernate, ActiveMQ, Jetty, CXF, Jersey, RESTEasy, Quarkus, Microprofile, Spring. Dig a little deeper, and you will find @JakartaEE APIs.

👇
Read 15 tweets
May 22
Every programming language provides ways to express our ideas and then translates them into reality. This thread will explore ten #Java programming features used frequently by developers in their day-to-day programming jobs.
Collections are used to store, sort, search, and iterate objects. It provides a few basic interfaces, such as List, Set, Map, and their implementations. The traditional way of creating Collections may look verbose. Therefore, #Java 9 introduced a few very concise factory methods.
#Java 10 introduced type inference for local variables, which is super convenient for developers.
Read 13 tweets
May 21
#Java has been and continues to be the most popular language over the last two decades. After many discussions with Java developers, I have concluded the following seven key reasons why Java developers still love Java after all these years.
1. Community: 
In every major city, you will find a Java user group that helps developers achieve the required skills, voluntarily and for free, who help distribute resources and solutions, increase networking, and expand #Java knowledge globally. dev.java/community/jugs/.
2. Language and Platform
An expressive and easy-to-read language helps new developers quickly get used to the existing codebase. #Java is an open-source programming platform with great documentation support. It's a platform that houses and enables a wide range of other languages.
Read 11 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(