Java 19 Virtual Threads - JEP Café #11
Ғылым және технология
JavaOne is back! ➱ oracle.com/javaone
What are virtual threads, the new model for threads, added to the JDK 19 as a preview feature by the Loom project.
⎯⎯⎯⎯⎯⎯ Chapters ⎯⎯⎯⎯⎯⎯
0:00 Intro
0:42 What are preview features, and how you can use them
3:34 What are Virtual Threads about?
4:21 Goals of Virtual Threads
5:11 Cost of a platform thread and CPU usage
7:49 How to use Virtual Threads
9:11 Using ExecutorServices with Virtual Threads
11:04 ThreadLocals and Synchronization
11:19 Synchronization, pinned threads and locks
13:14 Final words and outro
⎯⎯⎯⎯⎯⎯ Resources ⎯⎯⎯⎯⎯⎯
◦ The Dev.java website ➱ dev.java/
◦ JEP 425: Virtual Threads (Preview) ➱ openjdk.java.net/jeps/425
◦ JDK 18 ➱ openjdk.java.net/projects/jdk/18
◦ JDK 19 ➱ openjdk.java.net/projects/jdk/19
◦ OpenJDK ➱ openjdk.java.net/
◦ Oracle Java ➱ www.oracle.com/java/
Tags: #Java #Java19 #Loom #OpenJDK #JDK #JDK19 #ConcurrentProgramming #JEPCafe #InsideJava
Пікірлер: 81
I would love to see some patterns and real world problems solved with the new virtual threads 😊
@romanpleshkov1125
Жыл бұрын
golang?
Agree - some code in action with best practices will be super helpful.
yes, some code in action and best practices would be nice to see... Thanks!
Really like the explanation of the motivation, it addressed the question of "but why is this a problem when we have thread pools?" I think the preview feature explanation in the first few minutes could be removed though, it's not the first preview feature and I'm sure the whole idea of previews is explained elsewhere but still, a nice reminder.
Yes, please! It would be great to see some examples and patterns!
@java
2 жыл бұрын
Stay tuned please!
Great video. Love to see examples of Virtual Threads!
@java
2 жыл бұрын
More to come!
Wow... a super awesome explanation and I am really excited to explore Virtual threads.
@java
2 жыл бұрын
Great to hear!
Virtual threads are magnifique !
4 minutes into the video and finally you are on the topic.
welcome back, yes we would like to see examples
Gosh can't wait when I start use all this staff like semaphores and lock I learned for interviews!
Very informative video, thanks for publishing! 11:54 is very important point!
@VisruthCV
2 жыл бұрын
Here, the ReentrantLock should be declared as an instance variable, i.e. in the class of "key" passed in "synchronised (key)" otherwise it will not act as a replacement of synchronised block.
I'm super thrilled by Virtual Threads but I feel will take so long before using in PROD env
Some example and how things work under the hood would be amazing episodes.
Very good explanation 😀
@java
Жыл бұрын
Thanks! 😃
Nicely explained, thank you! Question what would be the impact of this on reactive programming?
Thanks!
Greate vid!! A thread reserves upfront 20MB in memory? can i get more detail information about it?
Good to see some example code
It would be nice to see how virtual threads work under the hood. How to handle exceptions in VT. Exception handling patterns. Are these preemptive or cooperative?
Of course, code examples are always good.
@java
2 жыл бұрын
Absolutely!
Great session. Thanks! I would like to see some code in action as well.
@java
2 жыл бұрын
Coming soon!
I am really happy to give you coffee from my side :D , but you should discuss patterns and ways of using virtual threads.
The end of Reactor pattern that i hate him , end to learn of go that i hate him also, I hope it will be in production as soon as possible, Thanks i love java !!!!!!
Because my English is not good, so I try to describe it as accurately as possible。 After consulting relevant materials and doing some practice, I did find that the JVM will replace blocked IO with NiO when it detects virtual threads. So my question is, since the virtual thread is running on the carrier thread, the carrier thread will still be blocked when the read() of NiO is executed, right? If the carrier thread is blocked as I said, the thread must be suspended. As a result, many thread switches will lead to a decline in CPU utilization. I really hope someone can solve my doubts. Thanks.
can javascript run on virtual threads in graalvm?
How does this affect happens-before ordering, especially on systems with weak hardware memory models?
@DavidDLee
2 жыл бұрын
Same JVM memory model
Can you please highlight some differences between Kotlin coroutines and virtual threads?
Has anyone written an in depth article of virtual threads with code examples,please share the article
Congrats!!! Very cool explanation! Next step: show me the code! 😁
@java
2 жыл бұрын
Stay tuned...
which extension has been dropped?
Isn't threads using something like 2 MB RAM for the stack each? 20 MB sounds way too much!
@ishcatu
2 жыл бұрын
Yes, I thought the same and according to Tomasz Nurkiewicz's talk at JChampion Conf it's 1MiB: kzread.info/dash/bejne/oJOMtLelmsnMpZM.html
@staffeng
2 жыл бұрын
I was also surprised by this. Wild stackoverflow searches tells me it's more in the lines of a few hundred KBs, like between 32KB to 512KBs. I don't know who's right here.
@DavidDLee
2 жыл бұрын
The Java stack is configurable, defaults are platform specific, but the range is something like 128K ~ 1M. The native stack is 2 MB by default for Linux. However, both these values are for virtual memory, not physical memory, so it's more of a limit rather than actual cost.
Coding in action would be great. Thank you!
Thank you for the informative presentation, and we wish to see code.
Certainly your coffee ended to early. I'm eager to hear more about the virtual threads.
@java
2 жыл бұрын
Please stay tuned!
Sir, Could you please use your holding hand to take a sip from coffee. I don't know why but I focused on that throughout the video :D
Show some examples with vthreads with blocking queries to db.
@DeepakLalchandaniProfile
2 жыл бұрын
Yes agreed please show us examples of virtual threads with blocking queries to db ?
Yes, I would like to see some example code about that ReentrantLock. Thank you for this video.
@VisruthCV
2 жыл бұрын
The ReentrantLock should be declared as an instance variable, i.e. in the class of "key" passed in "synchronised (key)" otherwise it will not act as a replacement of synchronised block.
@Java can you JEP and netcdf tutorial
I feel the "cost of a platform thread" needs a lot more explanation in a separate video -- perhaps with a few demos. I always read, most likely from incorrect sources, that thread creation and context switching is expensive in terms of CPU. I imagined it would in the order of a few tens of milliseconds. I'm surprised to learn that it takes just 1ms but the real cost is in the inability to 100s or 1000s of threads to keep the CPU busy. However, why is that in the apps I've worked on which had performance issues due to blocking networking calls I always see the CPU at near 100%? I thought it was due to all the thread context switching and garbage collection taking up the CPU more than my app's code but looks like it's not as per this explanation.
@LannisterFromDaRock
2 жыл бұрын
It's only a guess, but I think the 100% CPU usage was caused by iowait, especially if the OS was Linux.
@DavidDLee
2 жыл бұрын
I think the explanation was simplified but still pretty good. Could have mentioned cost of context switching, limited ability to schedule too many kernel threads. Could also mentioned limited capacity or diminished capacity when dependencies take longer to complete.
@bentels5340
2 жыл бұрын
Your CPU is probably at 100% because all of your threads are waiting -- it's constantly switching, trying to find a thread ready to do work.
Pooling threads in order to limit the number of conncetions to the DB doesn't sound like a good idea. Isn't it better to use a Semaphore in this case?
Need some code examples)
The code example at 10:50 is misleading. The semaphore doesn't actually do anything in this scenario and all queries will be executed sequentially. It would only make sense if you wrap the for-loop's content with `new Thread(()->{...}).start();`
@DeepakLalchandaniProfile
2 жыл бұрын
Could you provide a working java example for the one referred at 10:50
@jay_sensz
2 жыл бұрын
@@DeepakLalchandaniProfile ?! I did
@DeepakLalchandaniProfile
2 жыл бұрын
@@jay_sensz im mentioning about new Thread(()->{...}).start(); Can you share a working example please
@jay_sensz
2 жыл бұрын
@@DeepakLalchandaniProfile Semaphore semaphore = new Semaphore(10); for(Query query: queries){ semaphore.acquire(); new Thread(()->{ try{ query.run(); }finally{ semaphore.release(); } }).start(); }
@DeepakLalchandaniProfile
2 жыл бұрын
@@jay_sensz how to execute it using public static void main method ?? .do you have the full working example on github ?
golang and java
Can someone elaborare more on the resource utilisation at kzread.info/dash/bejne/nn-HtaSvgqfThMo.html