Java Multithreading Become A Master | Daniel McCarthy | Skillshare

Java Multithreading Become A Master

Daniel McCarthy, There is always more to learn

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
11 Lessons (47m)
    • 1. Multithreading in Java Introduction

    • 2. What is multithreading?

    • 3. Creating threads in Java

    • 4. What is a mutex?

    • 5. How to program mutexes in Java

    • 6. What is a semaphore?

    • 7. Programming semaphores in Java

    • 8. What are atomic variables?

    • 9. Atomic variables programming in Java

    • 10. What are thread pools?

    • 11. Creating thread pools in Java


About This Class

Learn how to speed up your java programs and multi-task once and for all with this easy but informative multi threading course.

In this course we discuss and preform

1. Multi-threading theory

2. Creating a thread in Java

3. Mutex theory

4. Creating a mutex

5. Semaphore theory

6. Creating a semaphore

7. Atomic Variable theory

8. Creating atomic variables

9. Thread pool theory

10. Creating thread pools.

After this course you should be on your way to knowing multi-threading well its important to follow this course to the end as multi-threading can be a complicated subject. I have made it as simple as possible for beginners stick with it and you will see results.


1. Multithreading in Java Introduction: So I'm gonna teach you guys threading multi threading. You probably already know a little bit about it to even be here If you don't. Don't worry, because I explain everything. So the first thing I'm gonna teach you is what is multi threading on? I need I need to teach you guys that for those who use or know what it is right after I teach you a multi threading is where they're going to create our own thread and java. So nice simple exercise. It's not difficult. Very easy, straightforward. Just make sure you follow that. What is threading video before you go to the programming? A thread lecture, Right. So after you've crazy first thread, I'm gonna teach you guys were, um utexas on. Um, you text only allows one thread at a time into a resource, right? You can use it to protect your program, because if you don't use mu Tex is on. You have multiple threads access in the same resource at the same time. It can cause concurrency problems on your program can basically break itself. Uh, So Mu Tex is a very important for taking back control. And, you know, after I've explained one UTexas to you. Guys will go around and program them on. You'll see how all that works in Java. After we learned about mu taxes, we're gonna learn about Sema Fours, so just stay tuned. I know this is a lot, but it's broken down into little steps. Just follow the course on a long makes sense after well, over seven fours we're gonna program some before is obviously yeah, And then we're gonna learn about Tomic variables again. Similar to Mu Tex is but not quite the same thing. You'll live mobile as soon. Well, then program atomic variables. So we we always after I speak about something, we program it just so you can understand and learn it Well, and then finally, we're gonna end the course on threat pools. You live or threat pools are so Yeah, Have a look 2. What is multithreading?: Hey, guys, we're going to discuss what multi threading is today. Firstly, less imagine you're writing a word document, and you are also listen to music on YouTube. The only reason this is possible is because of multi threading. So what exactly is multi threading? To understand this question, you first need to understand a bit about a computer's process. A computer's processor or central processing unit is responsible for interpreting instructions. It is the computers brain, if you like. It has a has access to the memory and hardware of your computer, but it is very dumb. It needs to be given instructions on what to do when you type on your keyboard. There is a computer program running on the process. Er, that reads the key. You have pressed without the CPU. The computer is just a box and nothing more So now we have covered what a process that is. What is a CPU call? A CPU call is responsible for interpreting the computers instructions. The more cause the computer has, the more conducive once think off CPU cause as workers that can all do the same thing. One CPU core can only do one thing at a time, but if you have a lot of them, you can do lots of things at once. Let's get back to our writing. A word document on listening to music on YouTube. Example. Both of these operations are handing lots of tasks. When you write your word document, it is constantly making sure you have not made a spelling mistake. Your Web browser. You're watching YouTube. It's the same. Browse e u. Maybe currently downloading a follow These programs create what is known as threads, which basically says, Run this bit of code separately. Think of it as allocating some work to your co workers in the office. The operating system handles these threads on, allocates them to threads on your process. Er, these threats are then run through a CPU call. The more cause you have, the more Koken be. Run it while at the same time. So the threads on your operating system are threats. Only opening system is aware of. It will pass the instructions to the rial threads on the process. The threads on a process are basically virtual CPU cause that past data to the rial CPU core Finally, the CPU cause responsible for interpreting the instructions given to it on the more CPU cause you have, the more you can process at the same time. So then what happens if you only have one CPU call? If you only have one CPU call, your computer will still give the illusion it is doing more than one thing at once. This works you to task. Switching each thread in the computer's operating system will be allocated with the given time frame before quickly switches to the next threat. This happened so fast that a human can not notice it on. This gives the illusion of multi threading. You know when things aren't quite going fast enough when you notice your computer freeze for a second or so multi threading is no automatic, and a software developer will have to program their software to make use of this powerful functionality on allow their software to perform multiple tasks. At the same time, software developers do this by creating threads in the operating system. Remember their operating system threads, which pass stated to CPU threads, which finally passed a to Cebu cause the point of multi threading is to allow more than one thing to happen at the same time in a computer system and allows increased performance as multiple cause concertos workload. In the next video, we'll discuss how to protect yourself from possible problems that can happen when creating threads. This next video will discuss Mu Texas Hope you found this useful. 3. Creating threads in Java: welcome. So this is a simple threading example in Java just to explain what I explained in the last video in code. Right? So in Java, threats can be created by creating a new threat. Class, thread, object like this. So threat to equals new threat. Right on inside. Here you pass a run a ball. So a rentable is an interface. Okay, so you see are threading example here. What I want to do is I'm going to implements, rentable and implement the methods run. So then, once you've created this new thread in its argument, passed new threading example. So, while they says is create a new threat, who's run a bull is threading example and then to start a thread. All you do is you go t dot Starks and it will start the thread. So what will happen is we start this threat we're saying are rentable is threatening examples. What we're saying to the thread is were saying create a new threat on call our run method. One threat is to be executed. So what happened is this will go to the Java virtual machine. It will say we're creating new threat. The new threat to be created on. Then you know, eventually our code, our code will be running on the CPU core, and it will be calling run. So just to demonstrate this, I'm gonna do a little bit of printing, right? Andi, I just want you to run that, okay? So you can see a prince. Hello. So what's happening is this is great news is creating a new thread which is then calling our run method. So if we just put a message underneath here just so I can demonstrate the flora of execution and you run that, you'll see ABC Hello. Now, even though we've swarmed our threat, we see ABC first. And that's very important for you to notice that because if we would if we was to see hello first on all the time, it would mean that it wasn't running on a separate threat. Right. But what's happening is we're creating a new thread. So we're saying we're saying, Go on, run this code Somewhere else were saying going running on another CPU coal. You know, I've got my own stuff to worry about, right? So essentially threats of workers, right? So it will run this code on a separate thread. So if we have, like five of these, you know they'll be run in no particular order. Right? So we run that now, you see, on if we keep running it, notice how the positioning changes for our ABC. That's because of the order. The threads, a run running, you know there's no order to it. Okay, so that s what that is, how you create thread and job, right? It's really that simple. So we create a new thread object pass are rentable. This then says to the job of virtual machine going, start our threat. We don't want to know about it. Just deal with the work in the background, right? And then that will call in the background. It will call this So even if we're waiting here in our main method, this is still be being cold because it's being run independently on another CPU core. So that's how you do threading in Java 4. What is a mutex?: So we're gonna learn about Mu Texas now. So, firstly, what is, um, you Tex while in multi threaded programming, Um, you Tex allows you to lock a part of a program from other threads on what this does is it prevents multiple threads access in the same resource at the same time To put simply what Mu Tex is our take the following example. Imagine we have three people on one toilet. The people, other threads. The toilet is the resource on the lock. Inside the toilet is the mu Tex. Now imagine all the people trying to get into the toilet first. They really have no manners. The person who gets in their first locks, the toilet, All the other people now have to wait for this person to unlock the toilet. In this example, the people being the threads have no order. They all want to get into the toilet when it's free on only when it's locked to they wait. This is a clear example of what mu taxes are. Our toilet resource is guarantee to only be used by one person as it is locked by the threat, accessing it on unlocked when the threat is done. All other threats have to wait as they obey the same rules. There is no order in which thread will use the toilet next. Once the current thread using the toilet has unlocked the mu texts, another threat can then lock them. You, tex. A real life example of using UM, you text would be with using a linked list on a single footed application. It's very easy to just add or remove elements from a linked list. Toe adds an element to the linked list. You simply get the last element of the linked list on, then said it's next bearable to point to the new element. You then set the last element on the linked list to this new element we have added. However, when you introduce multiple threads into the picture, you run into all sorts of problems because one thread maybe changing the last element on the linked list while the other is changing another's next bearable. You can run into all sorts of problems with multiple threads when changing a linked list. Parts of the less may even disappear, causing the program to lose where the elements are. But they were still exist in memory, leading to a memory leak. This can happen because two threads maybe adding an element of the list leading to the tail being changed. Since each thread is unaware of the changes to the list, the other thread is making. This can lead to one thread set in the tail to an element whose next bearable is no on not point into the element the other thread is creating. In this scenario, our resource is the linked list on. By locking our resource using, um, you text, we can avoid thes concurrency problems. 5. How to program mutexes in Java: In the last video, we discussed the theory of new taxes. I've now created a link list example to demonstrate why New Texas need to be used in multi threaded programming. So essentially, my linguist example consists of a mindless class that allows you to add strings to this list. Essentially, so how it works is people call the AB method on my list. It gets the root element. If it's no, then it sets the root element to the element we create by here. And essentially, each list element just contains a message on a current index on the current index is really just used for tracking reasons. So how it works is the list element has a message, a current index and next variable. So when you add to the list, you essentially just set the last elements next bearable. So the new list element you've just created on. Then, when you iterated through the route list element, you essentially say, get the current element said it to the root element, while the current element is not know, print the current element and then set the cone element to the next element. So we go currently was current on next. So that then allows you to reiterate through the entire list is a pretty simple example. Eso that even beginners should be here to understand. So if the rue element is no, we set the root element to the first element we've created just by here. If it's not know, then obviously we set We get the last element and said the next to the element we just created right here. And we increment the current index to help keep track on. We call print less when we're ready to print the entire list. Okay, so let's add some elements to the less just so I could demonstrate this. Okay, so we add Hello, world Cool. And then we print the less Let's run that and you see zero Hello, world one call. Okay, so I'm now going to create threads, okay? So make sure you watch the video on what threading is before you attempt this so essentially created you have a project OK on It doesn't have to be like this. This is this the project I've just put together to demonstrate why you Texas Air important and Moby 30 programming. So what? I want you to do is I want you to credit class. And I wanted to go implements Run nable. Okay, It's gonna ask you to create public void. Run. Okay, that's important as well. So once you've done that, just do list. Don't add hello. And we're just gonna you know, just copy this a few times. Okay? That's fine. So what I do here is I add hello to the less three times. Okay, um, so this run method is gonna be called by our threat. We haven't created our thread yet. If I run this, nothing's gonna happen, right? So it's great threatened Java just used the thread class. So thread t for the variable name equals new thread. And then you want to go new linked list example, or whatever it is in your case, basically, whatever. Whatever class has implemented rentable you should pass is the parameter to the thread, right? So, in my case is a new link list example and then you want to go t dot starts. So what they says is it says, go and create a new thread and call my run method on that separate thread. So if I run that now, all right. Okay. I don't see anything yet because I haven't printed the list. Right. So I put out here in the run list. Stop print list. You'll see. Hello. One. Hello to hello three. Okay, so that's fine, but we don't want to put this here. Okay? I'm gonna show you what you can do here. Now we're gonna sworn a few threats, so going to create a for loop, so this will correct five threads for us. Okay, good. So if we run that run that now, OK? If you keep running eventually, you'll even run into problems like no pointer. Exceptions. Explain more about that in a minute. Before we do that, we want to create an array list to keep track the threats. And I'll get to why, in a minute. So you just want ads, the threads, the threat you've just created till the array list. So what we're gonna do is write Essentially, what happens is we sworn these threats right on. Our program keeps running, so I go like that. You can see right if I go like this as well. Yes. So you can see it goes test test test test, ABC test. So I mean, method actually finishes before the threat does. So what we want to do is we want to tell the program toe wait until all threads of finished running and do not continue in our main thread until all the threats stop running on That way, our ABC will be out. Put it last. So how you do that is called joining you essentially loop through the threads we've created . So in our case, we're gonna look to the threads we've stored in our A list. They're the ones we've created, and you just go t dot join. And what that does is it goes through each threatened. It says, has this thread you're gonna have toe throws interrupted, exception as well. So what we do is we go through all our threads and we say, Has this thread finished yet? And if it hasn't, what happens is it actually waits above this line. This line here it waits in the joint method until that threat has finished. So if we run that now, we can see ignore the errors because we'll get to that in a minute. We can see ABC is always output last so this is important, right? So now we can do some think, like underneath here where we've joined up threads weaken go list top print list. So that'll only print the list when all the threads have finished adding to our list. Okay, so you can see that now, that's quite nice. Yeah. Now, now to explain why we occasionally when we run this, we get no point. Two exceptions. Well, the reason we get no point no exceptions is because we're not using Mu Tex is now. Remember what I said. Mu Tex is allow you to protect resources, right? So occasionally, some of these elements will be no on all sorts of things because there's lots of threads modifying the behaviour, this resource. At the same time, you have many threads of day in the current index, you have many threads adjusting these elements on their next variables on this is why occasionally the program breaks down. So if we run that again, look, sometimes it works. But look at that. We've actually even lost part of the list, which is what I said was possible in the last video, right? It's just disappeared. This is why I'm you Tex is are very important. They help you protect resource is because if all the threats start accessing a secure resource all the once on insecure resource I all at once it can make the whole thing fall down very dangerous because because you might be changing variables in the class that other stages are. Other lines in the program depend upon right, so that is the problem. So to fix this, we need to create Mu Tex. Now, in Java, every class, every object is every object can be locked. So essentially it is his own mu text. So in the ad method, just like synchronized. Okay, Now what synchronized does it locks this object, right? So when somebody else does, some think that causes the sink when someone else does. Synchronization in this class, such as, for example, has put sink and I see a to So if somebody calls, add, it locks this object. So then, if somebody calls print, lest at the same time, it waits for this object to be unlocked, that's really good, because what I means is only one person can Adam print at the same time. So, essentially, if we call ad and someone else cause print list print list or wait till this ad operation is completed. And if multiple threads air calling ad at the same time, they'll have to wait for each other. So if you have three threads calling ad, each thread will wait until this object are my list. Object is unlocked. So if we run that now, after we've put synchronized on ad and synchronized on print list, you'll see it is always in order and there's no longer any problems. And the reason for that is because we lock our object when we call these functions. So we basically say I'm adding an element you guys can wait. Don't try and add an element because I'm currently doing it and you're gonna cause problems . When it leaves this function, it unlocks the object, allowing other people to call at Now. It's very important that you do not Cole a function that, for example, if I corporate less here, this results in a deadlock. Okay, it can result in a deadlock, and what a deadlock is is when an object is already locked and you call something the locks it again in the same threat. If you lock the same object in the same threat. This concolor cause problems, especially when locking you Texas and C plus plus the C plus plus language. So that's very important to know that locking in the same thread can result. A deadlock on what a deadlock is is where the lock is locked and there's no way to unlock it. So the program just stoles essentially very dangerous. So try to avoid calling synchronised methods in the same thread. So in other words, add shouldn't call print list because there was synchronized. Now there's another way you can synchronize as well. You can use the synchronized keyword so I can say synchronized this And that is basically the same thing as putting the synchronize keyword here so I could go synchronized this. Okay, we run that now can see it works the same. Okay, See, print list doesn't seem to be causing any problems there. But do take that rule carefully because in C plus, plus lock in the same lock twice in the same thread can cause deadlocks very dangerous when when you end up in a situation where you lock the same object twice on, only unlock it. Once you can end up frozen, it's really dangerous. They also recommend you do not, um they also recommend you do not nest your synchronization because this could be really, really dangerous, resulting in deadlocks and program to stall. So I hope that covers up mu Texas for you. So essentially, we protect a resource by locking it, preventing all other threads from accessing it. So it doesn't matter how many threads are running when they call add it locks the object. So all other ads on the different threats there are frozen. Because essentially, when we sworn these threats, they're all running the code of the same time on different CPU cause right, and this is a problem, and this is why you need me to Texas to protect your data. 6. What is a semaphore?: Sam Fours and multi threaded programming can be used to restrict how Maney threats can access a resource at a time. You provide some affords with a maximum count, and some affords will wait until there is more room before letting you pass. The best way to demonstrate this is with a nightclub. In this case, our resource is the nightclub. Our bouncer or security guard is the semaphore on the men. Other threads take a look at this animation, where we let two threads into the club. In this example, the semaphore count is two, meaning only to threats can access our nightclub resource at the same time. The third thread shown is the man is forced to wait the semaphore one lame through only once. Another thread leaves the club. Can another threat take its place? Binary. Some before they're similar to mu Texas in that they have a count of one only alone. One threat to access a resource at a time. In this example, we have demonstrated counting semaphore that allows multiple threats to pass a really life example of usages of Sema. Fours could be with the server. You would use the same before to restrict the number of network connections to some service off where to prevent overloading the resource. This is a good example and could be used to help prevent denial of service attacks on a server by restricting the number of allow connections. 7. Programming semaphores in Java: welcome. So we're going to create some Simba Fours now on. It's gonna be completely based on the last video. So we're going to create a new project on Dive called it Simmer, for example. Okay. And inside that project, I wanted to create new class called nightclub, so we're gonna completely base it on the last video. Explaining what? Semaphore czar. Right. We're gonna create new semaphore variable here. Right. Okay, on and inside the constructor for nightclub. So just created constructive for it. We're going to initialize the semaphore to to So I was saying, Here is this semaphore has a maximum counter to, you know, just like in our last video, two threads could enter the nightclub. The third had to wait exactly the same thing. So what we do is to send before is basically gonna block when this count reaches zero and it will decriminalise each time somebody enters our semaphore. Right? So let's create a new method in our nightclub called Enter On. You know this will be entering the nightclub Right On we go this start Sem, don't quiet right on It also throws interrupted exception as well. So what's happening here is when someone calls enter, it will acquire the semaphore, causing the current count to go down. Okay, Andi. Then it will be one. Right? And once it reaches zero, it'll block. And it will. No set said, we say we have a print here. Right, Right. Well, happen is once once a semaphore reaches zero, it'll block here until someone releases the semaphore, causing the count to become above zero again. And this allows us to restrict how many people can access a resource of the time. Right? How many threats can access a resource of the time, So just create a a leave function now, and we're going to do the same thing. This stops m dot release. So instead of a choir release on, we're gonna go system dot out dot print line left the night club. Okay, cool. So we enter on, we acquire the resource, we leave and we release the resource. Right? We release the semaphore so acquired the semaphore, released the semaphore. So when we acquired the count drops one when we released the count increments one. Now, once the semaphore value zero, we block while we're acquiring. So this will not proceed until someone releases. Okay, So go back to your main class and just create a static variable here called nightclub. Okay. On in our main variable, a numbing method. We're gonna initialize it. Good. So we're going to create a thread now? OK, so all I wanted to do is go in your main class, just go implements rentable. Okay, on. Then implement all abstract methods and you'll see this public voice run, which is inside the rentable interface. If your i d doesn't order complete these, then just write out public voice run. So we're gonna swan five threats now, or men, if you want toe base it on the last example. Cause in the last example, the men were the threads. Right? So we go threads, t equals new threads. New semaphore Example. Now, this summer, for example, will be your class named by yours Might not be called. This the one that implements run a ball is the one you press here. Okay. And then we go t dot Start. So what's gonna happen is five threads, they're gonna be created, and then we're gonna start those threads. Andi, This run method of be called concurrently at the same time basically on different CPU cause , um so there will be many threats calling the function of the same time. Okay, so we go nightclub dot Enter and then we go nightclub Don't leave Okay on. And we're also going to try and run that now. So firstly, dio do a while true here as well because I want this to last forever. Okay, be easier to show. Show why in a minute some we press run and you see this goes on forever and ever and ever and ever blocks because we're always leaving the nightclub when we enter. Right? So what I want to do now is I want to comment at your nightclub. Don't leave and run the example again. You can see it's stuck. Enter the nightclub, enter the nightclub. So I let two people So our bouncer or semaphore right? It's now causing us toe. Wait at this semaphore Quiet. It's not allowing us to pass because the 74 is now zero. Okay, so only one is released. Will it allow us Teoh to go through? So some affords a powerful because they could be used. Teoh prevent overloading. Um, like prevent too many threads access in a resource of once such. Aziz, you might have a servant, right? So, you know, in a sequel server, for example, you have maximum connections. They could use some affords to make sure that this no more than those connections can get through. Assuming, assuming the connections are on separate threats. Right. And this is why semaphore zehr powerful Because they do allow you to restrict how many threats Connexus a resource of the time on it just helps not. It helps to know, overload the resource by doing stuff like this. Okay, so that is some force. 8. What are atomic variables?: atomic variables are variables that can safely be modified from different threads with atomic variables. Only one threat a time can change the value of the variable. So you are protected from concurrency problems. Think back to arm UTexas video, where we was protecting our objects from multiple threads on atomic variable is basically a class that has done that work for you, and you can safely change that variable. 9. Atomic variables programming in Java: in the last video, we learned about atomic variables when now we're actually going to start programming our own. So before we do that, I want to show you why we need atomic variables. And I've created a counter class to demonstrate this. In this counter class, we have a private interest your called counter on a simple method that just increments the counter. So counter plus plus, looks like one operation, right? But the problem is, it isn't. We specify counter, which gets the value of counter right. Then we go plus plus which adds one. And then we set the counter again to that value. Now, unfortunately, this means that multiple threads who would do encounter plus plus, it could have impacting results, different results each time because they may set the counts of arable to something else. Right, if we are to set method or whatever and then doing counter plus plus is no longer safe. So to get around this, we need to create an atomic variable. But first, let me show you how you create your own. So you understand it, right? We won't use any of the Java help classes right now. So to create your own atomic variable. Just go synchronized, void increment. So, again, this axes, um, you text and locks the counter object, right? And in here, I want you to go private. Volatile. Intra counter. Now, the reason for this is because the volatile basically says we want to make sure that any changes to this variable goes straight into memory. Um, one use multiple threads. You have CPU caches and oversee with multiple threads. If we're relying on this counter variable being up to date all the time, it's very important that gets written directly into memory on doesn't get cached anywhere. So specifying volatile allows this to happen. So this is no safe increment can be called from anywhere on. You know, if we had a set method, it would be fine. So go back to the main class, right? I wanted to create in the telegraph poles class. All right, Andi, you know, this is the main, the main class, right? I'm just gonna be dealing with it here. So we're now going to create atomic introduced so I can explain it, So just go static atomic in Georgia. Um, counter okay. On and at the import Berlin. Okay, so again, it's static because I'm accessing it Statically. Right. So we go counter equals new atomic, okay? And we're gonna provide zero there. So to put this simply create a counter, set the value to zero. Okay. And we're going to go counter dot sets fight? Yeah, if you want. That now works. I'm running my old project there. So he said that now, you know, build successful. This fine, right? There's other methods in here, too. You can go increment and get right on this. This works completely atomic, Lee. So if we go back to our counter is basically this really, you know, with some extra methods, and it locks the class, so that is atomic variables and java. 10. What are thread pools?: creating threads is time consuming. On the correction process is a waste of memory and CPU time. Creating too many threads can cause more harm than good. Using fretting can really speed up a computer program when done properly. You can use threading for time consuming operations such as encrypting a file. But for operations like this, it does not make sense to create more operating system threads than there are CPU course as at that point, there is no speed advantage. A common way of making sure you do not create too many threads for a particular operation is to use what is known as a threat poll a threat. A threat pool is a resource that is responsible for managing a certain amount of threats from an outside perspective. It holds the threats they're swimming in the pool. When you initialize a threat poor, you specify how many threads you want. This pool toe hold, and it will create them all on. Then start them all the threads will then wait until they have a new job to do. You then pass jobs to the pool, and the threads will process them. The blue circles in this animation represent the jobs being passed to the threads. The white circles are the threads. Many thread Paul implementations will block and wait. If you try to add a job to process when all the threads in the polar in use, this will then wait until a job in one of the threads is completed. Then the thread will process that new job. Other thread implementations will cue the jobs I won't block. Think of a doctor's surgery when all the doctors are occupied. You have to wait until your name is called. You're in a queue. Essentially, thread pools work the same way. Jobs can be cute, so in short, we use threat pools to avoid wasting unnecessary resources on CPU time. 11. Creating thread pools in Java: in the last video, we discussed threat polls When I we're gonna have a girl, were actually programming our own. So create a new project and go execute. Er okay. And just import that. Add an import for that. I'm gonna call it E X, because execute er's remember to import that as well. Okay. I'll get to get to that in a minute. On. I wanted to go. New fixed threat poll. OK, excellent. So here we create a new for pole with zero threads off the So we want more than zero threads. Right. Let's make a threat. Pool of four threats. Right? So in this case, it will allow it'll create four threads in the background for us, and we can pass jobs to run on those threats. Right? So then once you've done that, just go e x, don't execute and asks for a run. A ball. Okay, so just for this example, I'm gonna do this. Okay. Good. Yes. So, you know, there's other ways to pass rentable to this, but this will do So what will happen here is then if we go, system got out of prison line. Hello, Worlds. Right. If we run that. Now you see it once because we've only added one job, right? But now if I go here, do a four loop. I just said it just said it to five. Right. We're gonna loop five times because we've only got a pull the four. And I want to show you this. So we do that. 12345 Ah, that works. Now. Why does that work? Because the threads end. What happens is we pass the job in to our threat pool. It executes and prints Hello, world. And then the threat is finished with the job rights was free for other jobs, right? That's why we see five here now. What if What if thread is never free? What if What if we never finished the job, right? Run it again. Now we only see four. Because what's happening is it's is gone. The extra execute on and it's added it to the Q. Right, Because this is a non blocking threat. Pull this right. So, in other words, if we did this, it would reach this. It wouldn't block and wait until the threat was available. Right? So because we've only got a threat pull of four. And each thread does, while true loops forever. It only outputs in four times because the queue is constantly waiting for another threat to become free. Right? And just to explain this, you know, this is no different than implementing our renewable interface, where just creating a new new rentable and overriding it that way perfectly allowed. Um, see, if we run that now, we'll see our ABC. Right? So that just shows this non blocking. So this is this is the whole point of threat polls. So whenever you have a task to do, such is encrypting a file. Maybe you can split the input buffer divided by four. Okay. And then you can send those offsets off to a thread responsible for encrypting a section in the buffer. Right? And so, in our case, divide by four for four threads on. We passed that we pass those chunks toe. We create a new job on the threat poll that encrypts those chunks. Right. So chunks chunk one chunk to trunk three chunk for so then they will be processed in the background. Right? That's the whole point of the thread polls. If we want to add a chunk five while Sorry, we need a wait till we've processed at least one of the chunk. Right? And by doing this, you don't create a threat every time you have a job to do, which is really good, because it saves memory and CPU time. And it also allows the threads to have more CPU time as well. So great thing for apples are.