No video

Java 21 new feature: Virtual Threads

  Рет қаралды 60,946

Java

Java

Күн бұрын

Learn about Virtual Threads, how to use them and how they are working under the hood, a new feature brought to you by the JDK 21.
Virtual Threads are one of the most anticipated and exciting new feature of the JDK 21. They are a new model of threads, much lighter than the traditional platform threads. Virtual Threads are there to address the complexity and maintenance costs of asynchronous programming, without giving up on the performances this model gives. With virtual threads, you can get the best throughput performances with simple imperative blocking code. This episode tells you all about it: it shows you how you can use them, and how Virtual Threads are working under the hood.
⎯⎯⎯⎯⎯⎯ Chapters ⎯⎯⎯⎯⎯⎯
0:00 Intro
0:36 Why Virtual Threads have been added?
1:54 Analyzing the CPU usage of your blocking code
3:24 Why the "one request per thread" model cannot work
5:59 Solving this problem: less expensive threads or asynchronous code?
6:50 Introducing the asynchronous solution
9:36 Writing an imperative, blocking online shopping example
12:01 Making this code asynchronous the CompletableFuture API
13:49 Issues with the asynchronous code: debugging, testing, stack trace, exception handling, timeout handling
17:05 Making threads less expensive: by how much?
18:57 Creating and using virtual threads
22:50 How Virtual Threads are working under the hood
26:51 Running blocking code in a virtual thread
29:01 Handling native code and synchronized blocks: avoid pinning virtual threads
31:25 Wrapping up: virtual threads are cheap, blocking them is fine
33:01 Outro
⎯⎯⎯⎯⎯⎯ Resources ⎯⎯⎯⎯⎯⎯
◦ All the videos of the #RoadTo21 series: • Road to 21
◦ Upgrading from Java 17 to 21 #RoadTo21: • How to Upgrade to Java...
◦ The JEP 444: Virtual Threads ➱ openjdk.org/je...
◦ JDD 2018: Reactive programming: lessons learned by Tomasz Nurkiewicz ➱ • JDD 2018: Reactive pro...
◦ Java Virtual Threads by Sergey Kuksenko ➱ inside.java/20...
◦ Helidon Níma & Virtual Threads by David Delabassee & Tomas Langer ➱ inside.java/20...
◦ Java Asynchronous Programming Full Tutorial with Loom and Structured Concurrency ➱ • Java Asynchronous Prog...
◦ Launching 10 millions virtual threads with Loom ➱ • Launching 10 millions ...
#Java21 #Update #Java #OpenJDK #InsideJava #RoadTo21 #VirtualThread #Loom

Пікірлер: 140
@Parkhill57
@Parkhill57 10 ай бұрын
the piano is driving me crazy
@yemzibossa
@yemzibossa 11 ай бұрын
Your page becomes my number #1 source of learning Java faster. I love to always watch this channel. Thanks for all you do, Sir.
@JosePaumard
@JosePaumard 11 ай бұрын
Thank you!
@DoubleM55
@DoubleM55 11 ай бұрын
FInally! We can just let our threads block on IO, as God intended!!!
@SirSomnolent
@SirSomnolent 9 ай бұрын
My first thought watching this was "oh god, no".
@laurentjeanpierre3662
@laurentjeanpierre3662 11 ай бұрын
Thanks, Jose. I think your explanations are just at the right balance between technical and understandable. Your shorts are almost the only shorts I watch on KZfaq! Please keep on that way. I also need a copy of your mug of coffee, for it seems to empty itself only at the end of your task ;-). Merci !
@JosePaumard
@JosePaumard 11 ай бұрын
Thank you Laurent!
@andikovi1286
@andikovi1286 10 ай бұрын
Never heard anybody else explain in such a detailed and simple/friendly way. Congratulations!
@leolewis7754
@leolewis7754 11 ай бұрын
Thanks José for presenting this in such an easy way to understand, especially on the limitations of the system. I did read in some places that synchronized code would always be pinned to a platform thread which was detering me from investigating moving to jdk 21 to benefit from virtual threads given the fair amount of synchronized code we unfortunately have in our legacy applications. But I will now revisit that plan with the information you provided. Thanks again
@JosePaumard
@JosePaumard 11 ай бұрын
Thank you! Glad I could help!
@user-uu1rn8yb5d
@user-uu1rn8yb5d 11 ай бұрын
Thanks José for making this video, Really easy to understand the mechanics of Virtual Threads, how they work under the hood.
@JosePaumard
@JosePaumard 11 ай бұрын
Thank you!
@zoladkow
@zoladkow 9 ай бұрын
wow, an absolutely masterfully extended way of saying: "cheaper threads, to better hide the complexity of asynchroneous". I was just wondering why not Promises, but Virtual Threads (with essentially non-public async framework) instead. Now i know. Thanks
@AleksandarT10
@AleksandarT10 11 ай бұрын
Greatly and well presented. Keep up the good work Jose! We need more on this topic
@JosePaumard
@JosePaumard 11 ай бұрын
Thank you! I will publish more when we have the new version of Structured Concurrency. Stay tune!
@konstantinatwork3105
@konstantinatwork3105 11 ай бұрын
Awesome video. Very well structured and presented!
@java
@java 11 ай бұрын
Glad you liked it!
@MrSurprise777
@MrSurprise777 8 ай бұрын
Very great and detailed explanations of the topic! Thanks!
@andersonanjos4455
@andersonanjos4455 8 ай бұрын
If Mr. José Paumard is not an actor, maybe he should. This video is pretty funny!!! 😅 Nice to get a chance to delve into such complex content in a lighter and interesting way. Thank you, José 👏👏👏
@LawrenceDCodes.
@LawrenceDCodes. 11 ай бұрын
Excited to dig into this
@jackofnotrades15
@jackofnotrades15 5 ай бұрын
Amazing explanation...José Explaining both approaches and doing it so well with code is as always great.
@yordanov5.0
@yordanov5.0 6 ай бұрын
Magnificent explanation of pure engineering art. "Project Loom" is indeed a revolutionary masterpiece. 👏👏👏
@YvesDautremay
@YvesDautremay 11 ай бұрын
Merci José, super intéressant !
@JosePaumard
@JosePaumard 11 ай бұрын
Merci Yves!
@Braket21
@Braket21 8 ай бұрын
Perfect presentation ! Thank you ! Pretty that sonar don't support it yet .. hope it is coming soon.
@tuanm_
@tuanm_ 11 ай бұрын
Perfect! Thank you, Sir!
@PavanKumar-IT
@PavanKumar-IT Ай бұрын
Wow.. Loved your session. Super knowledgeable. Thank you very much
@RicardoSilvaTripcall
@RicardoSilvaTripcall 11 ай бұрын
Great Explanation !!!
@JosePaumard
@JosePaumard 11 ай бұрын
Thank you!
@shahzad4455
@shahzad4455 11 ай бұрын
José, I am becoming addicted to your cup of Java 🙂
@hbobenicio
@hbobenicio 10 ай бұрын
Amazing talk! Really complete and comprehensive! Thank you for that.
@RYahiaoui
@RYahiaoui 11 ай бұрын
Great Explanation ! Thank you
@JosePaumard
@JosePaumard 11 ай бұрын
Thank you!
@nickngn
@nickngn 8 ай бұрын
Nothing to say except: "Awesome!"
@rilwanj
@rilwanj 8 ай бұрын
Thank you, that was a great breakdown, I like to see more presentations from this guy again.
@usern90210
@usern90210 11 ай бұрын
Absolutely fantastique explainer as usual! Also so excited for Java 21 :D But as an aside, please, please, remove the white flashing transitions. It hurts my old eyes and I can think that folks with photo-sensitivity or epilepsy can have a hard time with these as well. Cheers, Niss
@JosePaumard
@JosePaumard 11 ай бұрын
Thank you Niss! I wasn't aware of this problem with flashing transitions, thank you for reporting. I will change that in the next videos.
@usern90210
@usern90210 11 ай бұрын
Merci Infinimenent@@JosePaumard Always a pleasure to follow your vids ^__^
@shurazeta
@shurazeta 11 ай бұрын
I like when this gentleman takes a sip of his coffe!!!
@D0gma
@D0gma 11 ай бұрын
Continue ton super travail José ;)
@JosePaumard
@JosePaumard 11 ай бұрын
Merci ! 🙂
@christoph9198
@christoph9198 11 ай бұрын
Very great explanation. Emphasized and repeated the important parts. 😀
@JosePaumard
@JosePaumard 11 ай бұрын
Thanks!
@royzor7965
@royzor7965 5 ай бұрын
Awesome video, thank you very much 😊
@soliveirajr
@soliveirajr 8 ай бұрын
How virtual threads are better than non-blocking I/O (Java NIO) created 20 years ago and extensively used today?
@user-nw2zm3qn5j
@user-nw2zm3qn5j 2 күн бұрын
nio requires so mich code change it mightnt worth it
@codeZarathustra
@codeZarathustra 11 ай бұрын
Great explanation master Gracias!
@cpthermes3703
@cpthermes3703 6 ай бұрын
He explained it so clearly. Bravo
@kenkrueger65
@kenkrueger65 11 ай бұрын
Thank you for this - that coffee must have been getting cold by the end
@JosePaumard
@JosePaumard 11 ай бұрын
Not quite, that's a good cup, it can keep the coffee warm for long enough 😉
@Congcoipro
@Congcoipro 11 ай бұрын
Thank you so much for your excellent presentation, Sir! Please give me some short explanations regarding the differences between Virtual Threads and the NIO mechanism. I appreciate your time.
@Panem78
@Panem78 6 ай бұрын
Very clear and nice explanations. Well done !
@ydexpert131
@ydexpert131 8 ай бұрын
Awesome explanations sir....Really nicely explained in plain simple language....
@2k5325i
@2k5325i 11 ай бұрын
Clarifying question: 28:43, is this saying IO inside parallelStreams is/was always a bad idea… or is the point that it’s only a bad idea *now that Virtual Threads exist*?
@adambickford8720
@adambickford8720 11 ай бұрын
Always been a bad idea. Remember, its using a thread *somehwhere* its just doing the bookkeeping for you. By default, you'll share the same thread pool w/all parallel streams in your app. That means you could starve the pool intended for CPU bound tasks (like `sum()`) while its waiting for IO
@2k5325i
@2k5325i 11 ай бұрын
@@adambickford8720 that makes sense. Just so I’m clear, for those at or below Java 17, is the recommended alternative to use a separate, dedicated thread pool for scheduling and orchestrating blocking IO?
@adambickford8720
@adambickford8720 11 ай бұрын
​@@2k5325i Exactly right. Its also perfectly fine to block those I/O threads as that's the intent and it won't 'starve' other threads. It normal to have far more I/O threads than cores. Just remember each thread takes memory and overhead to manage, even if idle. If you're CPU bound, more threads than cores makes things worse, not better, virtual or not. Remember that you'll potentially want to pool connections too and now you have to multiplex threads to connections. It never ends :)
@duquedehazard
@duquedehazard 11 күн бұрын
great explanations! thanks
@juanddperez
@juanddperez 6 ай бұрын
Oh là là, une magnifique explication! merci!
@felipemarchant4653
@felipemarchant4653 26 күн бұрын
"You're not doing I/O in your parallel streams, right?". Oh God, thankfully not. 😅 Very good video.
@hawaii3535
@hawaii3535 5 ай бұрын
Very great explanation! Thank you sir
@natbirchall1580
@natbirchall1580 11 ай бұрын
This is why I stick with java. The quality of people working on it is amazing.
@kartal_03
@kartal_03 2 ай бұрын
Great solution for i/o ops.
@Andi-dp9hy
@Andi-dp9hy 11 ай бұрын
Best loom explanation 👌
@alessandrobx5850
@alessandrobx5850 11 ай бұрын
Awesome explanation
@java
@java 11 ай бұрын
Glad you think so!
@Alberto_Cavalcante
@Alberto_Cavalcante 11 ай бұрын
Thanks José!
@L36654
@L36654 10 ай бұрын
Amazing feature! Thanks for the explanation too!!
@wernerjacobs3282
@wernerjacobs3282 9 ай бұрын
Good explanation as always, thanks a lot for these videos
@ievgen.official
@ievgen.official 3 ай бұрын
Thanks. Great explanation of main topic plus extra tips. My ears satisfied when I've heard about IO in parallel computation. You said that stack is copied into the heap space. Stack is stored in RAM in stack memory. To me Java can simply keep all stacks in place, and switch between them by overriding a pointer to current stack in OS thread. Does it really need to be copied?
@vVvvVVvvv373
@vVvvVVvvv373 11 ай бұрын
Does In-memory computation mean a task for CPU bound job? If so, computing some tasks with virtual thread isn't good cuz virtual thread has more overhead than planform thead. (of course virtual thread is way much better when it comes to blocking). Do I get right?
@JosePaumard
@JosePaumard 11 ай бұрын
Yes. Virtual threads are only interesting if your task blocks.
@srivatsvasu
@srivatsvasu 11 ай бұрын
wonderful presentation jose. cant wait to start using this. Why are we not supposed to call an IO operation or say a rest api calls in parallel streams? is there any reasons?
@JosePaumard
@JosePaumard 11 ай бұрын
Thank you! I/O operations in intermediate or terminal methods of streams: it's probably not working as you think. Same for parallel streams, you don't have any hand on how your data will be split, and to which point. So it gives a pretty unpredictable way of computing things. Pulling data from an I/O source: that's ok, this is what reader.lines() is doint. Don't do it in parallel though, it's useless.
@arielpapuga8908
@arielpapuga8908 10 ай бұрын
Is it true that virtual threads (called green threads) were already present in java 1.1 but then abandoned in 1.3 as too limiting? Could you please elaborate on what was consider the limitation then and how these limitations are overcome with this new implementation?
@JosePaumard
@JosePaumard 10 ай бұрын
Green threads were indeed an attempt at doing the same kind of things. But they were not working in the same way. One of the major difference was that a green thread was actually bound to a single platform thread, and could not jump from one another. There were other caveats, and in the end green threads were removed from the platform. Virtual threads are a totally new feature, with the same ideas in mind, but implemented in a complete new way.
@abdoub1656
@abdoub1656 10 ай бұрын
fisrt time i see you , i use to read "java le soir " blog articles !
@willemcheizoo521
@willemcheizoo521 10 ай бұрын
José thanks for this clear explanation!
@vuongbui4433
@vuongbui4433 6 ай бұрын
If my project is using java8 and upgrade to java21, will my old blocking code run in a virtual thread be pinned back to the platform thread? (old code includes old libraries like JDBC, GRPC, REST CLIENT)
@AbhishekKumar-xr1ss
@AbhishekKumar-xr1ss 4 ай бұрын
can anyone explain what does "that would include the probable context switching that you will have to pay" mean at 28:12 ?
@sombriks
@sombriks 11 ай бұрын
obrigado José!
@botondvasvari5758
@botondvasvari5758 4 ай бұрын
why dont you introduce async await instead completedfuture mess ? :D
@russellskalden8064
@russellskalden8064 11 ай бұрын
Very nice new feature! Thanks. Is there some chance to see in Java mechanism like in C# async/await?
@Iulefaitdesvidos
@Iulefaitdesvidos 11 ай бұрын
No, virtual threads were created not to have the async await pattern. It solves this bad pattern in languages, where there are async methods and others not, because when you start to have a method with async it contaminates the others. Like when you use reactive patterns with Flux, RxJava, CompletableFuture..
@cinlung
@cinlung 10 ай бұрын
Your coffee makes me wants to make a cup of coffee :)
@kwhandy
@kwhandy 11 ай бұрын
he holding java coffee is just 💥💥💥
@RahulSingh-cl3wm
@RahulSingh-cl3wm 9 ай бұрын
One stupid question may be, What a raw implementation of creating a platform/kernel thread would like? If I want to create a platform/kernel thread without depending on Thread class how can i do it? Just curious to know if it is documented somewhere to read internals.
@wanlizhang6584
@wanlizhang6584 7 ай бұрын
Thanks your great talk! so... should we worry about the platform threads are enough, how can we observe it, eg: queue size, active thread number...
@nejathakan5521
@nejathakan5521 11 ай бұрын
Read my earlier post please and please help me to answer following question:"Why does Java sais that it invented something called virtual threads, when it was invented many years ago for C? How can something that is allready invented be invented again?"
@JosePaumard
@JosePaumard 11 ай бұрын
Sorry but I don't think Java ever said that it invented Virtual Threads. Where did you saw that?
@nejathakan5521
@nejathakan5521 11 ай бұрын
@@JosePaumard Sorry for my not correctly using the english language. I am not used to it 100%. I try my best. You are right that Java can't speak :-))) What I meant was the project and or company of course. I repeated looking the video again. It is said that JDK introduced Virtual Threads. So this does not mean that they invented it. So you are right virtual threads are not an invention of Java (project/company). Thank for helping to clearify this point. In short:"Virtual threads were not invented by JAVA (project/company.) Best regards.
@JosePaumard
@JosePaumard 11 ай бұрын
@@nejathakan5521 We agree: Java can't speak :-))) Best!
@csm2526
@csm2526 8 ай бұрын
ForkJoinTask and ForkJoinPool is tgere to do light weight concurrent Thread execution but why Virtual Threads again?
@geneb.4301
@geneb.4301 11 ай бұрын
I've been working with Reactor for three years now, and while Reactor is an excellent framework, the price to pay for creating and maintaining asynchronous software is huge. This change comes not a moment too soon.
@floradreamweaver3646
@floradreamweaver3646 11 ай бұрын
tickering reactor is a lot of fun, but not so much when comes to production and teamwork. The virtual thread makes reactor useless to me the moment I found it.
@LostLuma
@LostLuma 11 ай бұрын
The captions on this seem to be for a different video, kinda confusing 😶
@JosePaumard
@JosePaumard 11 ай бұрын
Thank you for reporting it! It should be fixed now.
@mwigojonathanmark1683
@mwigojonathanmark1683 10 ай бұрын
Clean code, makes wonder what I have always been writing 😢
@hareepjoshi
@hareepjoshi 2 ай бұрын
where can i get that java cup, please?
@gerdsfargen6687
@gerdsfargen6687 9 ай бұрын
Merci Beucoup
@joelfankam3719
@joelfankam3719 10 ай бұрын
thx for this introduction
@alyxgurr755
@alyxgurr755 11 ай бұрын
Coffee cup, that's just classy
@reapi_com
@reapi_com 6 ай бұрын
Best ever.
@sadiulhakim7814
@sadiulhakim7814 2 ай бұрын
12:49 In this mess how can i check that?
@JijilKakkadathu03
@JijilKakkadathu03 5 ай бұрын
Wow!
@joecaruso3756
@joecaruso3756 8 ай бұрын
Great video thank you
@alexparker7791
@alexparker7791 11 ай бұрын
how do you launch 2 or more io ops in parallel and wait for both like when parts of your data flow fan out and fan in
@JosePaumard
@JosePaumard 11 ай бұрын
That's the job of structured concurrency. It is still in preview in 21, reason why I chose not to cover it here. You can watch how it is working on this channel: kzfaq.info/get/bejne/aNR_nZt-sK7anas.html
@--Nath--
@--Nath-- 11 ай бұрын
Hrm.. so would reactive mechanisms need to bother with all that they do? Or just use these for the blocking stuff?
@JosePaumard
@JosePaumard 11 ай бұрын
Use them for the blocking stuff. And ask yourself: do you need to write your code in an async way. Maybe you don't.
@--Nath--
@--Nath-- 11 ай бұрын
@JosePaumard yeah, it does seem like these frameworks do a lot of complicated stuff to decouple concurrent tasks from threads to try deal with blocking stuff, which is what virtual threads do.
@JosePaumard
@JosePaumard 11 ай бұрын
@@--Nath-- Exactly.
@yilativs
@yilativs 10 ай бұрын
good explanation
@Gaiwwie
@Gaiwwie 8 ай бұрын
frontend with java.. why not?
@failing_gracefully
@failing_gracefully 11 ай бұрын
Measure. Don't guess.
@nordeenhasan6030
@nordeenhasan6030 11 ай бұрын
but this feature isnot new. it was produce in java 19
@JosePaumard
@JosePaumard 10 ай бұрын
Yes. The new thing is that it is now a final feature. And there were some updates too.
@FurkanAliUnal
@FurkanAliUnal 11 ай бұрын
Can I have a cup of ur coffee sorry I am too excited about features
@sadiulhakim7814
@sadiulhakim7814 2 ай бұрын
You had been drinking that tea for 33 mins. Wasn't that cold?
@pavnn2347
@pavnn2347 3 ай бұрын
content is too good, however the piano background music is driving me crazy. Anyway thanks Jose
@pawsdev
@pawsdev 9 ай бұрын
Is any guess when Oracle will make normal modern docs site like Kotlin, JS,Python has? JavaDocs is ugly
@VuLinhAssassin
@VuLinhAssassin 11 ай бұрын
How many cups of coffee have you been drinking, sir?
@JosePaumard
@JosePaumard 10 ай бұрын
One. Just one, really 😄
@VuLinhAssassin
@VuLinhAssassin 10 ай бұрын
@@JosePaumard Your presentations are amazing, sir. Keep up the good work 👍
@JosePaumard
@JosePaumard 10 ай бұрын
@@VuLinhAssassinThank you!
@viswanathankalyanasundaram9315
@viswanathankalyanasundaram9315 3 ай бұрын
Thank you .. but could you please please please mute the background music as it is distracting and irritating. The music is unnecessary for this type of content. Content itself is great but the music makes it feel like when you are studying for an exam and you hear blast of TV noise coming from the other room.Thanks again
@motionattached
@motionattached Ай бұрын
Maybe low the volume little bit or some other types of music.
@cccc2740
@cccc2740 11 ай бұрын
subtitles are totally unrelated...🤣🤣
@JosePaumard
@JosePaumard 11 ай бұрын
I just fixed them. Sorry for the inconvenience.
@michelvandermeiren8661
@michelvandermeiren8661 10 ай бұрын
Performance vs classical threads is so disappointing. There is nearly no diff
@JosePaumard
@JosePaumard 10 ай бұрын
There is no reason that your code run faster on virtual threads. In the end, it is a platform thread that executes your virtual thread. What you should run in a virtual thread is I/O operations. Then you will get better performances, because blocking is handled in a much better way than asynchronous code.
@iaintoft7003
@iaintoft7003 9 ай бұрын
would have been nice to have a video without the music 😅
@karthiksundaram544
@karthiksundaram544 2 ай бұрын
😂
@user-uh1yi3xx6s
@user-uh1yi3xx6s 5 ай бұрын
Please remove background music.
@aliyevruslan936
@aliyevruslan936 10 ай бұрын
Why Germans are so good at everything
Java 21 JVM & GC Improvements #RoadTo21
12:09
Java
Рет қаралды 16 М.
Look at two different videos 😁 @karina-kola
00:11
Andrey Grechka
Рет қаралды 8 МЛН
Parenting hacks and gadgets against mosquitoes 🦟👶
00:21
Let's GLOW!
Рет қаралды 11 МЛН
Алексей Щербаков разнес ВДВшников
00:47
Java 21 API New Features #RoadTo21
16:48
Java
Рет қаралды 40 М.
Java 8 to 18: Most important changes in the Java Platform
31:24
Virtual Threads in Java 21
17:22
Will Tollefson
Рет қаралды 3,9 М.
Java 21 Is Good?! | Prime Reacts
27:08
ThePrimeTime
Рет қаралды 228 М.
Java 21 Pattern Matching Tutorial #RoadTo21
23:28
Java
Рет қаралды 26 М.
Look at two different videos 😁 @karina-kola
00:11
Andrey Grechka
Рет қаралды 8 МЛН