JDK 19 | Java Virtual Threads | Detailed Explanation With Example | JavaTechie

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to Java techy so I believe you all heard a word called virtual thread if not don't worry I am here to tell you virtual thread is a new feature introduced in jdk 19 as a preview feature which will helps us to improve an application availability throughput and code quality on top of reducing memory consumption the concept of virtual thread has gained considerable attention in recent times and many programming languages are updating their thread library to support the virtual trading feature so in this tutorial I will provide a deep introduction to the virtual thread with internal and also will understand how virtual thread is different from our current thread architecture okay so without any further delay let's get started [Music] foreign [Music] so before Java 19 there are two types of thread operating system thread or you can also called it as a kernel thread and the other one is the platform thread so this operating system thread are managed and scheduled by the operating system itself however the platform thread cannot be directly executed it must be mapped to a operating system thread so in the end operating system thread is the one which execute every instruction in the computer system so when you usually create a thread it creates a platform thread and then it make one to n mapping with an operating system thread to perform any task If You observe this current thread working principle is fine right there is a one-to-one mapping between a platform thread and operating system thread and we are able to execute concurrent tasks then what's wrong with it why Java developer introduce another thread called virtual thread don't worry I will explain everything before you know why we need virtual thread or what is the advantages of using virtual thread let's understand thread for request architecture so that it will easy for me to differentiate virtual thread and platform thread okay now let's assume my application hosted to one of the application server like Tomcat or JBoss and each server will have their default thread pull so that bulk request can concurrently Handle by individual thread now here all the threads wet in the pool for a new request to come so once the new request comes the thread picks up the request and it makes application call to solve that particular request okay once request sent to the backend application then thread needs to wait until you received response back from the back end once the response comes back again the thread will return back to the thread pool this is the typical thread for life cycle model or thread for life cycle architecture so if You observe carefully thread is only used while process the request and rest of the time it is basically waiting or doing nothing which is not good right now if you will relate this architecture from thread perspective whenever a platform thread is created an operating system thread is allocated to it only when the platform thread exits the jvm this operating system thread is free to do other tasks until then it cannot do any other task according to this thread per request model the operating system thread will be unnecessarily locked down while he is not doing anything since operating system thread are precious and finite resources its time is extensively wasted in this platform thread architecture now if you look into jvm memory management we have various segment to manage different type of instance we have method area where all the static and class related metadata is assigned in that particular area and we have Heap area where it allocate memory to the object or to the instance next to that there is something called Java stack area okay this is the place where thread allocate its memory if memory is already allocated to the thread and if he is not doing anything or simply sitting ideal then it's waste of memory right so if You observe in this platform thread architecture we are having lot of challenges to increase the throughput of our application now how we can overcome it or what is the best way to deal with multi-threaded environment to achieve the proper CPU utilization that is where this virtual thread have been introduced in jdk 19 in order to efficiently use underlying operating system threads rather than blocking them so let's understand the same thread per request model with virtual thread architecture then you will understand how this virtual thread feature is so powerful and it's recommended to use in Enterprise application okay now the scenario is same we have a application hosted to the web server and that particular server is having their own thread pool with couple of thread but in Virtual thread the structure is bit different or you can say the memory management is bit different I will compare the memory management in Virtual thread and platform thread in Virtual thread architecture all the virtual thread will be residing in Java Heap memory like a normal object so when request comes to the application server then it won't directly picked up by the platform thread present in the thread pool rather it will picked up by a virtual thread then virtual thread will be ascending to a platform thread and then platform thread will again Ascend to the operating system thread now the request will be handled by the virtual thread only when it process the request otherwise virtual thread will be residing just as an object in the Java Heap memory okay now you might have a question if virtual thread is going back to hip memory then what will happen to the platform thread on operating system thread that's the key factor here if there is no real work executing then all the thread will detach from each other that is what the out of the box feature given in Virtual thread now keep a note virtual thread are mapped to the platform thread when it does the real work unnecessarily virtual thread will not going to assign to any platform thread operating system threads are not allocated to the platform threads until real work needs to be executed if there is some work must need to be executed then only platform thread will assign to the operating system thread so these two are the key concept of virtual thread okay that is the reason this virtual thread is lightweight and it is easy to use in multi-threaded environment you can spin up 10 million of virtual thread but you cannot spin up the 10 million platform thread that is how it is lightweight so again just have a quick recap if any request comes virtual thread will be assigned to the platform thread which turns use operating system thread for execution that's pretty clear so you understand platform thread and virtual thread architecture with internal right so let's Deep dive one step ahead to do high level benchmarking of CPU utilization in both the case I mean platform thread and virtual thread so I will explain in the platform thread then you will understand what is the disadvantages then why do we need the virtual thread okay so to perform high level benchmarking consider a web server deployed on a machine with 16 GB of RAM and each thread needs 20 MB of ram it means we have 16 GB Ram capacity and each thread needs 20 MB then how many threads with this 16 GB my system can accommend it 16 into 1000 by 20 which is 800 thread by using this 16GB Ram I can spin up 800 thread on my system now let's assume to process a single request my system took 100 millisecond 0.1 millisecond for the request preparation 0.1 millisecond for response processing and the entire time it took 100 millisecond to complete or to process one single request now let's assume I have received 800 concurrent requests now I have 800 thread and a 800 request so it means one thread can handle one request right it seems the thread count reached its maximum capacity because we already spin up all the possible thread that is 800 and total number of request 800 so each thread can handle one request that is how we just trying to analyze okay now let's calculate the total CPU time for a single request so to calculate the total CPU time for a single request request preparation took 0.1 millisecond and response processing took 0.1 millisecond so the total will be 0.2 millisecond for a single request now we have 800 requests right for 800 request it will take 800 into 0.2 millisecond which will be 160 millisecond next just evaluate the CPU utilization per one second okay now to evaluate the CPU utilization total CPU time for a single request is 0.2 millisecond and for 800 request it is 160 millisecond right so 160 and 1000 millisecond which is just a second which is 16 percent now okay now if you look into this number in a second only 16 percent of CPU was utilized this shows that we are under utilizing the CPU we are not optimally utilizing the CPU at its capacity now if You observe carefully sixteen percent of CPU is being utilized if you are running 800 thread now how many thread I need to utilize the CPU 90 percent how to calculate the logic is simple right so number of thread required is four thousand five hundred but I have 16 GB Ram with 16GB Ram I can only trigger 800 threads this shows that our backend server can actually capable of handling 4500 request per second by utilizing 90 percent of the CPU due to the constraint we are able to create only 800 thread now if I want to spin of 4500 threads then I need to increase my ram capacity how much RAM is required to create 4500 threads so considering each thread required 20 MB of Ram 4500 into 20 90 GB so we need 90 GB of RAM to trigger 4500 threads to achieve ninety percent of CPU utilization this number is crazy isn't it hence with platform thread we are not able to utilize the hardware at its full capacity but you can overcome this if you are using virtual thread now as you understand each thread need 20 MB to process but if you are using virtual thread it will take very light memory in some bytes okay even you don't look into the MB so you can spin up million of thread with limited Ram capacity and you can improve your CPU utilization as well okay fine I believe we are good with the theoretical understanding now let's get our hands dirty with virtual thread code and whatever the theory we have discussed so far we'll try to prove few of them in the code itself okay and also in my upcoming session we'll do the proper benchmarking using J meter or we will just visualize the memory management using the any any third party tool like J profiler or jvisual BM I will come with the different tutorial to do the proper benchmarking also in coming tutorial we'll integrate it with our springboard app okay but for now I'll just give you some heads up about how you can create the virtual thread and whether it really depends on any platform thread or not to process it and how much time usually it takes to spin up a thread I mean few of the statistic I will show you with the virtual thread code also you will understand how you can create the virtual thread okay let's move into the installation your idea then make sure that you have installed jdk 19 or higher version of Feed so if You observe I already installed jdk 20 for my machine if I will show you the see here I have jdk 20 with me okay make sure to use jdk 19. since this is the preview feature I'll show you how you can mark it as a preview and one more note make sure you should use the latest intelligent version if you are using any version of 2021 then I don't think it will work because I tried with my old IntelliJ IDEA it support up to zk17 I didn't find any option for jdk 19 so keep a note try with the latest IntelliJ IDEA version okay now let me quickly create a class to demonstrate this virtual thread virtual thread demo but before I write anything in this class make sure you need to mark this feature as a preview either in your palm.xml like this add this particular plugin specify the target jdk 20 or what version of jdk you are using and make sure to specify the argument doubled as enabled as preview okay you can add it in the palm.xml or you can add it in the edit configuration that is up to you but I have added in the plugin now let me minimize this Define the main method then I will show you how we can create the virtual thread and platform thread using this jdk 19 okay so thread is a class and there is a factory method of virtual can you see here a virtual and also you will find of platform okay so first let's create the platform thread and then you want to start this thread now or you want to unstarted it later when you need you want to start and join for now I want to start it okay and if You observe the argument of start is runable so I can pass the Lambda and I can print something platform thread and the thread name fine now I will just add a line here Now to create virtual thread you can use same thread dot of virtual okay again you have option either start it now or do it later and also pass the runnable in both the case the start method and unstart Method argument is same which is runable because runnable is the way to create the thread and whether you want to make it platform or virtual that is up to you or it depends on the method signature what you have written here so also I just want to pre print this fine now let me run this If You observe the output here this is what the platform thread okay thread name is Main and if you see the virtual thread what we are printing here name of the virtual thread which is virtual thread 21 and if you check the next literal first joint pull dash one dash worker dash one this is nothing your platform thread okay this literal pointing to your platform thread from the fork join so what it means this virtual thread is running on top of this particular platform thread okay this is what we discussed right so if You observe here the virtual thread will run on top of platform thread then again platform thread will run on top of os thread so virtual thread we found the literal and platform thread it will get from the fork join this is what the literal okay now next let's try to understand we'll create couple of three let's say 10 thread then we'll try to understand whether the virtual thread if there is no work whether it is it will be available or it will go and stay in the Java Heap memory or it will Target to the different platform thread okay this is what the theory you understand right now we'll understand it in code so let me add few line of code I mean simple javoid stream okay let me comment this for now so if You observe here this is very simple statement in stream of range 0 to 10 I want to create 10 thread and then I'm just doing map to object and inside that map to object I am creating the virtual thread but I am not starting it now so what I am doing on this unstarted I am giving this runnable task and what I am doing in this test nothing simple statement I am just printing the zero thread okay and its name then I am going to sleep for 10 second then after 10 second I just want to verify whether the virtual thread what I am creating is pointing to the same platform thread or it is depending on other platform thread okay then I am just converting to list since I am creating the 10 thread object and I am iterating them and starting each thread and again I am iterating and joining each thread very simple statement okay to just save our time I just copy paste the code but you can give a try there is nothing wrong so this index I will change it to the something like ah th count thread count okay now let me run this piece of code so if you'll see the output here very interesting just try to understand this literal okay so virtual thread 20 initially it started executing then we have applied If You observe you let me move it down if You observe here we just add the thread slip for 10 second intentionally we are blocking the thread okay so initially virtual thread started on top of platform thread first joint pull one worker one then it went for 10 second block then immediately virtual thread release the lock or it got release the platform thread first then pull one worker one and again it took the help of this particular platform thread first join pull one worker two okay so first it begin with this platform thread then when this virtual thread went to the block it went to the Java Heap memory when it come back it again assigned to the different platform thread not the same one can you see here this is your core one and worker 2 both are different platform thread so clearly we can see that whenever the virtual thread is blocked it releases the platform thread and its stack is moved to the Heap memory now the platform thread can be pinned to another virtual thread to carry out its task okay when the blocking operation is finished the virtual thread will be pinned to any free platform thread and will Mount its stack on that particular platform thread that is what it's doing here fine the virtual thread is the same because the first thread I am printing here so the thread is same but it is trying to use the different platform thread when it is going to the block okay this is the next statement we have understand here right it released the or each of the thread will be detached from each other that is what we understand right when there is a blocking so this is clear now the next step what I want to show here we want to spin of thousands of virtual thread and platform thread and we will see how much time each thread is taking to execute or whether the virtual thread is going to be fast or platform thread is going to first that is a simple demo okay so I will modify in the same code first I will Define system current time in a second then I'll just Define bar let's say for now 1000 okay I'll just Define here fine then I don't want to write anything any code because I just want to print out the time taking for each thread execution I mean two spin of thousand virtual thread how much time it is taking and to spin thousand platform thread how much time it is taking okay this is the simple thing I am going to demonstrate so what I will do I will again find the end time system dot current time millisecond then I'll just print millisecond used to Launch thread count or total thread n minus start okay simple code right fine let me Zoom this here now if I run this piece of code did I comment okay I modified it right so now if I'll run this piece of code I am trying to create 1000 virtual thread okay we'll see let me run this class for thousand virtual thread it took 97 millisecond okay let me add another zero ten thousand for ten thousand it tooks 184 millisecond for one lakh let's see it is taking 511 millisecond okay I mean to create one lakh thread it is taking very minimum time but let's try same using the platform thread okay of platform to trigger 1000 thread sorry one luck thread let's see how much time it is taking can you see the output here to trigger or to initiate one luck thread it tooks eleven thousand eight zero nine millisecond see the difference in case of virtual thread it is only taking five eleven millisecond but in case of platform thread it is taking 11809 millisecond which is near to the 11 second right now if I'll reduce 1 0 for 10 000 let's see how much time it is taking it is taking one double nine four millisecond but this if I'll make it virtual thread it will take very minimum time because the virtual thread is lie to it that is the reason it was able to spin up quickly okay so this is another difference what we understand in the theory that it is very expensive to manage or to maintain platform thread but virtual thread is lightweight and you can see the proof here right so really these Java 19 virtual thread feature is simply awesome and thanks to the project Loom guys they are doing the great job by introducing this kind of virtual thread feature to achieve better concurrency and to improve the throughput with the lightweight thread okay also in my upcoming session I will do couple of more video on Virtual thread once I will get more familiar with the internal virtual thread once we'll do the benchmarking or virtual thread okay do let me know in a comment section if you guys have any doubts that's all about this particular video guys thanks for watching this video meet you soon with A New Concept
Info
Channel: Java Techie
Views: 13,790
Rating: undefined out of 5
Keywords: java virtual threads, java 19 virtual thread, java 20 virtual threads, java virtual threads project loom, java virtual thread executor, java virtual threads performance, java virtual threads vs Platform thread, jdk19, javatechie, virtual thread
Id: z17QckCZ6fA
Channel Id: undefined
Length: 25min 27sec (1527 seconds)
Published: Sat Jul 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.