Greetings, welcome back. Scott Stanlick here. In these lessons we're gonna take a look at threading. We'll look at the Java main thread. We're gonna have a peak at various thread life cycles. We will take a look at thread execution pools and thread schedulers. In this particular lesson, we're gonna start off by taking a look at threads proper. This is typically where you would start in a discussion on threads. So the first thing I wanna show you is my machine that I'm running on. I have four processors so this is a four core machine. We can see that I have 236 processes or apps or process IDs running in 2,906 threads. So there are many, many threads on the machine, many more threads than there are processes and certainly more threads than there are processors. And so these threads they go through state changes. They're runable, they're waiting, they're blocking, they're timed out, they're sleeping. We will see, we'll see how that all unfolds here in just a minute. So first off, what is a thread? Well, a thread is a lightweight process that can be run independently by the operating system. In fact, there can be many threads all running simultaneously. It's kinda like a multi lane highway versus a single lane road. So we can run multiple threads concurrently or in parallel. Therefore, getting much more work done in the same amount of time. So the first thing I wanna show you here is we've created a class called file upload thread. So sure enough it is a thread, which is to say it has a run method. So what this thread is going to do will be determined by what is coded in the run method. Because the thread's lifespan is the length of time it takes to run the run method. So there's our thread. It's going to simulate a file upload. So the first test it says threading done wrong and let's see what that looks like. When I run this, it should go out and figure out how many processors I have, which we just saw I have four processors. So we're not overloading the hardware here, we're creating no more threads than what we have processors at this moment. So I'm calling the run method on the thread, four of them actually here. And I can see that all of these threads ran on the main method at a priority of 5. And that it took 2.2098 seconds. Well, that doesn't look like threading to me. That looks like these things were stacked up single file. And they were. They were because you don't call the run method directly. You call the thread start method. And the start method engages the machinery such that background tasks are created and spawned. So let's do it properly and let's use the start method on thread and let's see what that same scenario looks like. Available processors is still four, and so I'm going to see the same four file loads, the same four file uploads rather, but now you can see it's 0.547 seconds. And I can also see that they did not all run on the main method. This was Thread 1, Thread 2, Thread 3, Thread 0. So basically, they were each independent threads, and now we can see that we did the same amount of work in a fourth of the time. So the next guy we're going to take a look at is how we can name threads. Like for instance, this is kind of difficult to figure out what's what, right? Especially when you're looking at stack traces and thread dumps and that sort of thing. So what I wanna do is again the same scenario, but I wanna set the name of the thread to knowledge-city-number and it will just use the "I" in the four loop here. So we should see knowledge-city-number 1, number 2, number 3, number 4. And again, we're starting these correctly. Now, the operating system will arbitrarily run these threads. And that is because we haven't set a thread priority. So they're all running it priority 5. So they each have equal priority. So there's no saying necessarily which thread will run first. And as you can see here knowledge-city-number 1 ran first, 0 ran last actually. And if we run it again you would see an array of of varieties there. It's unpredictable, the order in which they're gonna run. Unless you do this of course. If you set the priority, if you have a particular thread like let's say it's printing customer invoices and the customer is standing there at the counter, you probably want the thread that's doing the printing to have a higher priority, to give a higher execution priority. And so what we're doing here is the same idea. We're gonna run this file upload four times, except three of them are gonna have a priority 9. I'm just doing a little switcheroo here. If "I" is 3, then we're gonna set the priority to 5. If "I" as anything other than 3, we're gonna set the priority to 9. So what we should see is knowledge-city-number 3 runs last. Because the other three had a higher priority so let's just prove that. And it did. So we can see knowledge-city-number 2, number 1, number 0. They were all running at a priority 9 and they trumped this knowledge-city-number 3, which was running at priority 5 so it was served last. Thank you for watching.