SlideShare a Scribd company logo
1 of 47
Project Loom
OR
How to Be Happy With Blocking
Code
@AndriiRodionov
https://blog.softwaremill.com/synchronous-or-asynchronous-and-why-wrestle-with-wrappers-2c5667eb7acf
https://www.baeldung.com/thread-pool-java-and-guava
BlockingQueue
Blocking request processing!
Problems
Problems
• Wasting thread-pool threads
• Wasting memory
Solutions
• Add more threads to thread pool?
• To be non-blocking?
• Project Loom?
Million Threads Baby
Why it fails?
Non-blocking request processing
McDonald's Kiosks
What means to be non-blocking
• An “event” should trigger a code execution -
callback
• How we can describe or register this callback?
– Callbacks
– Wrappers
– Coroutines
https://blog.softwaremill.com/synchronous-or-asynchronous-and-why-wrestle-with-wrappers-2c5667eb7acf
Future -> CompletableFuture
Java 5:
• Future<T>.get() - blocking
Java 8:
• CompletableFuture<T>.thenApply(...).... - non-blocking
• ~70 CompletableFuture methods to be non-blocking
Code readability
• Statements is now encoded as method calls
HttpClient.newBuilder()
.build()
.sendAsync(request,
HttpResponse.BodyHandlers.ofString())
.thenAccept(response -> . . .);
.thenApply(. . .);
.exceptionally(. . .);
• If we loved this style of programming, we would not have statements
in our programming language and code in Lisp
To be Reactive or not to be?
• If your Spring MVC application works fine - don’t touch!
• Imperative programming is the easiest way to write,
understand, and debug code
• Reactive and non-blocking generally do not make
applications run faster
Spring-framework-reference
To be Reactive or not to be?
• Reactive in some cases (WebClient to execute remote calls in
parallel) can make applications faster
• On the whole, it requires more work and can increase slightly
the required processing time
To be Reactive or not to be?
The key benefit of reactive and non-blocking is:
• the ability to scale with a small,
• fixed number of threads
• and less memory
Back to Green threads
Green Threads
– class Thread - @since 1.0
– M to N (M:1) mapping
– Threads scheduling - Java
– stop(), destroy(), resume(), suspend()
– When a green thread blocked, its carrier thread was also
blocked, preventing all other green threads from making
progress
– Loom is a (partial) return to the idea of green threads
https://www.youtube.com/watch?v=oBMZc2VMujw
class Thread
– At Sun’s Solaris OS with Java 1.2/1.3 - 1:1 threads mapping
– Threads scheduling - OS
– stop(), destroy(), resume(), suspend()
• @Deprecated(since="1.2", forRemoval=true)
– In 1998 the Sun Java Web Server (the precursor of Tomcat)
run each request in a separate thread, and not an OS process
– Able to serve thousands of concurrent requests this way
https://www.youtube.com/watch?v=oBMZc2VMujw
1:1 costs
• Native thread creation time
– Native thread
• Thread stack size - 1-2Mb
• Native threads time-slice scheduling
– Context switching (Memory/Cache/Registers)
Problems with blocking
Imperative programming
Problems:
• Wasting thread-pool threads
• Wasting memory
Project Loom way:
• Do not use thread pool
• Make thread instantiation fast
• Do not consume much memory
Virtual threads
• Exists only in Java runtime
– no 1-to-1 wrappers over OS threads
• Creating a virtual thread is cheap and fast
– they should never be pooled
– no OS-level data structures for the stack - no memory wasting
• Blocking a virtual thread is cheap
• Threads scheduling - Java
– no OS context switching
https://www.youtube.com/watch?v=oBMZc2VMujw
Carrier threads
• Virtual threads must be attached to OS thread to execute
• Carrier threads - OS threads upon which a virtual thread
executes
• Over virtual thread lifetime, a single virtual thread may
run on several different carrier threads
• When a virtual thread blocks (when a blocking call such as
I/O is made), it is “parked” and another virtual thread runs
on the carrier thread
https://lukstei.com/2020-07-25-a-first-look-into-the-project-loom-in-java/
Virtual thread 1
…
parked/blocked
Virtual thread 2
…
Virtual thread 1
…
Carrier
Thread 1
Carrier
Thread 2
Stack is saved
Stack is restored,
execution continues
Time
Carrier threads
Virtual thread creation
• class Thread
Thread thread = Thread.startVirtualThread(runnable);
• unbounded executor -
ExecutorService exec =
Executors.newVirtualThreadExecutor();
exec.submit(runnable1);
exec.submit(runnable2);
Demo
1M threads example
Sleeping threads
https://blog.softwaremill.com/synchronous-or-asynchronous-and-why-wrestle-with-wrappers-2c5667eb7acf
Virtual threads scheduler
• The default scheduler is the work-stealing scheduler from
ForkJoinPool
• Work-stealing schedulers work well for threads involved:
– that normally process in short bursts and block often
• Long running (without blocking) virtual threads can be pinned
to its carrier
– none of the existing schedulers for virtual threads uses
time slices to preempt virtual threads
Demo
Pinned threads
STRUCTURED CONCURRENCY
• When multiple threads run without coordination, it’s spaghetti
code
• Should use different approach to coordinate them
• In the 1960s, structured programming replaced goto with branches,
loops, and functions
STRUCTURED CONCURRENCY
• Structured concurrency should do the same for concurrent tasks
• We should know, from reading the program text, when they all
finish
https://vorpus.org/blog/notes-on-structured-concurrency-or-
go-statement-considered-harmful
Demo
Structured concurrency
try(var ex = Executors.newVirtualThreadExecutor()) {
ex.submit(() -> run());
ex.submit(() -> run());
} // Blocks and waits
Open questions
• Thread local
• Thread dump
• Threads communication
– BlockingQueue
– Channel (Carrier)
• Structured concurrency
• Supported API
– https://wiki.openjdk.java.net/display/loom/Blocking+Operations
– except synchronized/wait/notify and local file I/O operations
Cheap threads
can do expensive things
1M virtual threads illusion
• Loom does allow you to have many threads, even 1,000,000 threads,
• but ...
• not if those threads have deep stacks
Stack Trace From Hell
https://dzone.com/articles/filtering-
stack-trace-hell
1M virtual threads illusion
For deep stack it increase
• total memory usage for stack entries and
• also comes at a cost of long garbage collections
https://webtide.com/do-looms-claims-stack-up-part-1/
Why Thread Pools?
- Why Small Thread Pools?
- We can create up to 50K native threads in a pool!
- A limited thread pool is a coarse grained limit on all
resources, not only threads
- Limiting the number of threads puts a limit on concurrent
lock contention, memory consumption and CPU usage
https://webtide.com/do-looms-claims-stack-up-part-2/
Conclusion
• blocking code is much easier to write
• virtual threads are very fast to start and cheap to block
• for “real world tasks” we should anyway limit the number of
virtual threads
1994
“He (Bill Joy) would often go on at length about how
great Oak would be if he could only add closures and
continuations and parameterized types”
“While we all agreed these were very cool language
features, we were all kind of hoping to finish this
language in our lifetimes and get on to creating cool
applications with it”
“It is also interesting that Bill was absolutely right about
what Java needs long term. When I go look at the list of
things he wanted to add back then, I want them all. He
was right, he usually is”
Patrick Naughton,
one of the creators of the Java
To read
• https://inside.java/tag/loom
• https://www.javaadvent.com/2020/12/project-loom-and-
structured-concurrency.html
• https://blogs.oracle.com/javamagazine/going-inside-javas-project-
loom-and-virtual-threads
• https://blog.softwaremill.com/will-project-loom-obliterate-java-
futures-fb1a28508232
• https://webtide.com/do-looms-claims-stack-up-part-1/
• https://vorpus.org/blog/notes-on-structured-concurrency-or-go-
statement-considered-harmful/
Comparing APIs
API Type No value Single value Multiple values
Standard Java –
Synchronous APIs
void T Iterable<T>
Standard Java –
Asynchronous APIs
CompletableFuture
<Void>
CompletableFuture
<T>
CompletableFuture
<List<T>>
Project Reactor
implementation of
Reactive Streams
Mono<Void> Mono<T> Flux<T>
https://devblogs.microsoft.com/azure-sdk/async-programming-with-project-reactor/

More Related Content

More from GlobalLogic Ukraine

GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”
GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”
GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”GlobalLogic Ukraine
 
GlobalLogic QA Webinar “What does it take to become a Test Engineer”
GlobalLogic QA Webinar “What does it take to become a Test Engineer”GlobalLogic QA Webinar “What does it take to become a Test Engineer”
GlobalLogic QA Webinar “What does it take to become a Test Engineer”GlobalLogic Ukraine
 
“How to Secure Your Applications With a Keycloak?
“How to Secure Your Applications With a Keycloak?“How to Secure Your Applications With a Keycloak?
“How to Secure Your Applications With a Keycloak?GlobalLogic Ukraine
 
GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...
GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...
GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...GlobalLogic Ukraine
 
GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...
GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...
GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...GlobalLogic Ukraine
 
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”GlobalLogic Ukraine
 
Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"
Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"
Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"GlobalLogic Ukraine
 
GlobalLogic Webinar "Introduction to Embedded QA"
GlobalLogic Webinar "Introduction to Embedded QA"GlobalLogic Webinar "Introduction to Embedded QA"
GlobalLogic Webinar "Introduction to Embedded QA"GlobalLogic Ukraine
 
C++ Webinar "Why Should You Learn C++ in 2021-22?"
C++ Webinar "Why Should You Learn C++ in 2021-22?"C++ Webinar "Why Should You Learn C++ in 2021-22?"
C++ Webinar "Why Should You Learn C++ in 2021-22?"GlobalLogic Ukraine
 
GlobalLogic Test Automation Live Testing Session “Android Behind UI — Testing...
GlobalLogic Test Automation Live Testing Session “Android Behind UI — Testing...GlobalLogic Test Automation Live Testing Session “Android Behind UI — Testing...
GlobalLogic Test Automation Live Testing Session “Android Behind UI — Testing...GlobalLogic Ukraine
 
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...GlobalLogic Ukraine
 
GlobalLogic Azure TechTalk ONLINE “Marketing Data Lake in Azure”
GlobalLogic Azure TechTalk ONLINE “Marketing Data Lake in Azure”GlobalLogic Azure TechTalk ONLINE “Marketing Data Lake in Azure”
GlobalLogic Azure TechTalk ONLINE “Marketing Data Lake in Azure”GlobalLogic Ukraine
 
GlobalLogic Test Automation Online TechTalk “Playwright — A New Hope”
GlobalLogic Test Automation Online TechTalk “Playwright — A New Hope”GlobalLogic Test Automation Online TechTalk “Playwright — A New Hope”
GlobalLogic Test Automation Online TechTalk “Playwright — A New Hope”GlobalLogic Ukraine
 
Java Webinar #13 “Where Is My Development Zone?”
Java Webinar #13 “Where Is My Development Zone?”Java Webinar #13 “Where Is My Development Zone?”
Java Webinar #13 “Where Is My Development Zone?”GlobalLogic Ukraine
 
NET Webinar #1 "Is There a Life Outside the Entity Framework"
NET Webinar #1 "Is There a Life Outside the Entity Framework"NET Webinar #1 "Is There a Life Outside the Entity Framework"
NET Webinar #1 "Is There a Life Outside the Entity Framework"GlobalLogic Ukraine
 
GlobalLogic С/C++/Embedded Live Coding Challenge. Basic graph algorithms
GlobalLogic С/C++/Embedded Live Coding Challenge. Basic graph algorithmsGlobalLogic С/C++/Embedded Live Coding Challenge. Basic graph algorithms
GlobalLogic С/C++/Embedded Live Coding Challenge. Basic graph algorithmsGlobalLogic Ukraine
 
Online TechTalk “Flutter Mobile Development”
Online TechTalk “Flutter Mobile Development”Online TechTalk “Flutter Mobile Development”
Online TechTalk “Flutter Mobile Development”GlobalLogic Ukraine
 
Online TechTalk  "Patterns in Embedded SW Design"
Online TechTalk  "Patterns in Embedded SW Design"Online TechTalk  "Patterns in Embedded SW Design"
Online TechTalk  "Patterns in Embedded SW Design"GlobalLogic Ukraine
 
Сloud Webinar #1 “Architecture of Highly Loaded Geo-Distributed Applications”
Сloud Webinar #1 “Architecture of Highly Loaded Geo-Distributed Applications”Сloud Webinar #1 “Architecture of Highly Loaded Geo-Distributed Applications”
Сloud Webinar #1 “Architecture of Highly Loaded Geo-Distributed Applications”GlobalLogic Ukraine
 
Сloud Webinar #2: “PCI DSS Compliance: Getting Ready for the Certification”
Сloud Webinar #2: “PCI DSS Compliance: Getting Ready for the Certification”Сloud Webinar #2: “PCI DSS Compliance: Getting Ready for the Certification”
Сloud Webinar #2: “PCI DSS Compliance: Getting Ready for the Certification”GlobalLogic Ukraine
 

More from GlobalLogic Ukraine (20)

GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”
GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”
GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”
 
GlobalLogic QA Webinar “What does it take to become a Test Engineer”
GlobalLogic QA Webinar “What does it take to become a Test Engineer”GlobalLogic QA Webinar “What does it take to become a Test Engineer”
GlobalLogic QA Webinar “What does it take to become a Test Engineer”
 
“How to Secure Your Applications With a Keycloak?
“How to Secure Your Applications With a Keycloak?“How to Secure Your Applications With a Keycloak?
“How to Secure Your Applications With a Keycloak?
 
GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...
GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...
GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...
 
GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...
GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...
GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...
 
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
 
Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"
Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"
Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"
 
GlobalLogic Webinar "Introduction to Embedded QA"
GlobalLogic Webinar "Introduction to Embedded QA"GlobalLogic Webinar "Introduction to Embedded QA"
GlobalLogic Webinar "Introduction to Embedded QA"
 
C++ Webinar "Why Should You Learn C++ in 2021-22?"
C++ Webinar "Why Should You Learn C++ in 2021-22?"C++ Webinar "Why Should You Learn C++ in 2021-22?"
C++ Webinar "Why Should You Learn C++ in 2021-22?"
 
GlobalLogic Test Automation Live Testing Session “Android Behind UI — Testing...
GlobalLogic Test Automation Live Testing Session “Android Behind UI — Testing...GlobalLogic Test Automation Live Testing Session “Android Behind UI — Testing...
GlobalLogic Test Automation Live Testing Session “Android Behind UI — Testing...
 
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
 
GlobalLogic Azure TechTalk ONLINE “Marketing Data Lake in Azure”
GlobalLogic Azure TechTalk ONLINE “Marketing Data Lake in Azure”GlobalLogic Azure TechTalk ONLINE “Marketing Data Lake in Azure”
GlobalLogic Azure TechTalk ONLINE “Marketing Data Lake in Azure”
 
GlobalLogic Test Automation Online TechTalk “Playwright — A New Hope”
GlobalLogic Test Automation Online TechTalk “Playwright — A New Hope”GlobalLogic Test Automation Online TechTalk “Playwright — A New Hope”
GlobalLogic Test Automation Online TechTalk “Playwright — A New Hope”
 
Java Webinar #13 “Where Is My Development Zone?”
Java Webinar #13 “Where Is My Development Zone?”Java Webinar #13 “Where Is My Development Zone?”
Java Webinar #13 “Where Is My Development Zone?”
 
NET Webinar #1 "Is There a Life Outside the Entity Framework"
NET Webinar #1 "Is There a Life Outside the Entity Framework"NET Webinar #1 "Is There a Life Outside the Entity Framework"
NET Webinar #1 "Is There a Life Outside the Entity Framework"
 
GlobalLogic С/C++/Embedded Live Coding Challenge. Basic graph algorithms
GlobalLogic С/C++/Embedded Live Coding Challenge. Basic graph algorithmsGlobalLogic С/C++/Embedded Live Coding Challenge. Basic graph algorithms
GlobalLogic С/C++/Embedded Live Coding Challenge. Basic graph algorithms
 
Online TechTalk “Flutter Mobile Development”
Online TechTalk “Flutter Mobile Development”Online TechTalk “Flutter Mobile Development”
Online TechTalk “Flutter Mobile Development”
 
Online TechTalk  "Patterns in Embedded SW Design"
Online TechTalk  "Patterns in Embedded SW Design"Online TechTalk  "Patterns in Embedded SW Design"
Online TechTalk  "Patterns in Embedded SW Design"
 
Сloud Webinar #1 “Architecture of Highly Loaded Geo-Distributed Applications”
Сloud Webinar #1 “Architecture of Highly Loaded Geo-Distributed Applications”Сloud Webinar #1 “Architecture of Highly Loaded Geo-Distributed Applications”
Сloud Webinar #1 “Architecture of Highly Loaded Geo-Distributed Applications”
 
Сloud Webinar #2: “PCI DSS Compliance: Getting Ready for the Certification”
Сloud Webinar #2: “PCI DSS Compliance: Getting Ready for the Certification”Сloud Webinar #2: “PCI DSS Compliance: Getting Ready for the Certification”
Сloud Webinar #2: “PCI DSS Compliance: Getting Ready for the Certification”
 

Recently uploaded

Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 

Recently uploaded (20)

Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 

Project Loom or how to be happy with blocking code

  • 1. Project Loom OR How to Be Happy With Blocking Code @AndriiRodionov
  • 6. Problems • Wasting thread-pool threads • Wasting memory
  • 7. Solutions • Add more threads to thread pool? • To be non-blocking? • Project Loom?
  • 11. What means to be non-blocking • An “event” should trigger a code execution - callback • How we can describe or register this callback? – Callbacks – Wrappers – Coroutines
  • 13. Future -> CompletableFuture Java 5: • Future<T>.get() - blocking Java 8: • CompletableFuture<T>.thenApply(...).... - non-blocking • ~70 CompletableFuture methods to be non-blocking
  • 14. Code readability • Statements is now encoded as method calls HttpClient.newBuilder() .build() .sendAsync(request, HttpResponse.BodyHandlers.ofString()) .thenAccept(response -> . . .); .thenApply(. . .); .exceptionally(. . .); • If we loved this style of programming, we would not have statements in our programming language and code in Lisp
  • 15. To be Reactive or not to be? • If your Spring MVC application works fine - don’t touch! • Imperative programming is the easiest way to write, understand, and debug code • Reactive and non-blocking generally do not make applications run faster Spring-framework-reference
  • 16. To be Reactive or not to be? • Reactive in some cases (WebClient to execute remote calls in parallel) can make applications faster • On the whole, it requires more work and can increase slightly the required processing time
  • 17. To be Reactive or not to be? The key benefit of reactive and non-blocking is: • the ability to scale with a small, • fixed number of threads • and less memory
  • 18. Back to Green threads
  • 19. Green Threads – class Thread - @since 1.0 – M to N (M:1) mapping – Threads scheduling - Java – stop(), destroy(), resume(), suspend() – When a green thread blocked, its carrier thread was also blocked, preventing all other green threads from making progress – Loom is a (partial) return to the idea of green threads
  • 21. class Thread – At Sun’s Solaris OS with Java 1.2/1.3 - 1:1 threads mapping – Threads scheduling - OS – stop(), destroy(), resume(), suspend() • @Deprecated(since="1.2", forRemoval=true) – In 1998 the Sun Java Web Server (the precursor of Tomcat) run each request in a separate thread, and not an OS process – Able to serve thousands of concurrent requests this way
  • 23. 1:1 costs • Native thread creation time – Native thread • Thread stack size - 1-2Mb • Native threads time-slice scheduling – Context switching (Memory/Cache/Registers)
  • 24. Problems with blocking Imperative programming Problems: • Wasting thread-pool threads • Wasting memory Project Loom way: • Do not use thread pool • Make thread instantiation fast • Do not consume much memory
  • 25. Virtual threads • Exists only in Java runtime – no 1-to-1 wrappers over OS threads • Creating a virtual thread is cheap and fast – they should never be pooled – no OS-level data structures for the stack - no memory wasting • Blocking a virtual thread is cheap • Threads scheduling - Java – no OS context switching
  • 27. Carrier threads • Virtual threads must be attached to OS thread to execute • Carrier threads - OS threads upon which a virtual thread executes • Over virtual thread lifetime, a single virtual thread may run on several different carrier threads • When a virtual thread blocks (when a blocking call such as I/O is made), it is “parked” and another virtual thread runs on the carrier thread
  • 28. https://lukstei.com/2020-07-25-a-first-look-into-the-project-loom-in-java/ Virtual thread 1 … parked/blocked Virtual thread 2 … Virtual thread 1 … Carrier Thread 1 Carrier Thread 2 Stack is saved Stack is restored, execution continues Time Carrier threads
  • 29. Virtual thread creation • class Thread Thread thread = Thread.startVirtualThread(runnable); • unbounded executor - ExecutorService exec = Executors.newVirtualThreadExecutor(); exec.submit(runnable1); exec.submit(runnable2);
  • 32. Virtual threads scheduler • The default scheduler is the work-stealing scheduler from ForkJoinPool • Work-stealing schedulers work well for threads involved: – that normally process in short bursts and block often • Long running (without blocking) virtual threads can be pinned to its carrier – none of the existing schedulers for virtual threads uses time slices to preempt virtual threads
  • 34. STRUCTURED CONCURRENCY • When multiple threads run without coordination, it’s spaghetti code • Should use different approach to coordinate them • In the 1960s, structured programming replaced goto with branches, loops, and functions
  • 35. STRUCTURED CONCURRENCY • Structured concurrency should do the same for concurrent tasks • We should know, from reading the program text, when they all finish https://vorpus.org/blog/notes-on-structured-concurrency-or- go-statement-considered-harmful
  • 36. Demo Structured concurrency try(var ex = Executors.newVirtualThreadExecutor()) { ex.submit(() -> run()); ex.submit(() -> run()); } // Blocks and waits
  • 37. Open questions • Thread local • Thread dump • Threads communication – BlockingQueue – Channel (Carrier) • Structured concurrency • Supported API – https://wiki.openjdk.java.net/display/loom/Blocking+Operations – except synchronized/wait/notify and local file I/O operations
  • 38. Cheap threads can do expensive things
  • 39. 1M virtual threads illusion • Loom does allow you to have many threads, even 1,000,000 threads, • but ... • not if those threads have deep stacks
  • 40. Stack Trace From Hell https://dzone.com/articles/filtering- stack-trace-hell
  • 41. 1M virtual threads illusion For deep stack it increase • total memory usage for stack entries and • also comes at a cost of long garbage collections https://webtide.com/do-looms-claims-stack-up-part-1/
  • 42. Why Thread Pools? - Why Small Thread Pools? - We can create up to 50K native threads in a pool! - A limited thread pool is a coarse grained limit on all resources, not only threads - Limiting the number of threads puts a limit on concurrent lock contention, memory consumption and CPU usage https://webtide.com/do-looms-claims-stack-up-part-2/
  • 43. Conclusion • blocking code is much easier to write • virtual threads are very fast to start and cheap to block • for “real world tasks” we should anyway limit the number of virtual threads
  • 44. 1994 “He (Bill Joy) would often go on at length about how great Oak would be if he could only add closures and continuations and parameterized types” “While we all agreed these were very cool language features, we were all kind of hoping to finish this language in our lifetimes and get on to creating cool applications with it” “It is also interesting that Bill was absolutely right about what Java needs long term. When I go look at the list of things he wanted to add back then, I want them all. He was right, he usually is” Patrick Naughton, one of the creators of the Java
  • 45. To read • https://inside.java/tag/loom • https://www.javaadvent.com/2020/12/project-loom-and- structured-concurrency.html • https://blogs.oracle.com/javamagazine/going-inside-javas-project- loom-and-virtual-threads • https://blog.softwaremill.com/will-project-loom-obliterate-java- futures-fb1a28508232 • https://webtide.com/do-looms-claims-stack-up-part-1/ • https://vorpus.org/blog/notes-on-structured-concurrency-or-go- statement-considered-harmful/
  • 46.
  • 47. Comparing APIs API Type No value Single value Multiple values Standard Java – Synchronous APIs void T Iterable<T> Standard Java – Asynchronous APIs CompletableFuture <Void> CompletableFuture <T> CompletableFuture <List<T>> Project Reactor implementation of Reactive Streams Mono<Void> Mono<T> Flux<T> https://devblogs.microsoft.com/azure-sdk/async-programming-with-project-reactor/