Java threads ๐Ÿงต

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how's it going everybody let's bro here hope you're doing well and in this video I'm going to be teaching you guys about threads in Java so let's get into it [Music] if you find this video helpful please remember to Like comment and subscribe your support will help keep this channel running everyone so for this video we're going to be discussing threads this is going to be part 1 of 2 where this video is going to be focusing on explaining what threads are and part 2 will be multi-threading so a thread is defined as a thread of execution in a program I know that kind of sounds redundant it's kind of like a virtual CPU and the JVM the java virtual machine allows an application to have multiple threads running concurrently so each thread can execute parts of your code in parallel with the main thread and each thread has its own priority threads with higher priority are executed in preference compared to threads with a lower priority so with the Java Virtual Machine it will continue to execute threads until either one of the following occurs the exit method of class runtime has been called or all user threads have died then the program stops basically so when the JVM starts up there is a thread which calls the main method this right here and this thread is called main so it's kind of like the thread that starts or begins your program so let's do some practice with the main thread so with threads we can check to see it how many are active with a certain method so we're going to type in the name of the thread class dot and use the active count method and this returns how many threads are currently active however we need to put this within a print line statement so we can actually see it so I'm just going to system that out dot print line and place thread dot active count within here and let's see how many threads are running and it says one thread is currently running and that is our main thread basically and we can also check to see the name of this specific thread so we can use thread dot current thread dot get name so this is going to return the name of the main thread basically and then we'll also need to put this within a print line statement so let's just copy this and paste it within here and I'll probably just turn this into a comment for now all right so the name of the thread that is currently running is named main just like we had in that description just previously we can also change the name of this thread too so we can use the set name method of the thread class so we're going to type in thread and then we want the current thread which is main so thread dot current thread and then what we're gonna type is dot set name and we can name this whatever we want so how about main for no good reason so when we display the name of the current thread it's going to be main with a bunch events it's probably not a practical name I was just being silly so all threads have a priority and we can check to see what a threads priority is so if we want to check the priority of our main thread what we're gonna type is thread dot current thread which is referring to the main thread dot get priority and then of course we need to put this within a print line statement so system dot out dot print line then I'm just going to place this within here and the priority of our main thread is 5 so this is on a scale between 1 and 10 the higher the number the higher priority for this thread so we can actually change the priority for at thread so let's change the priority for our main thread so I'm just going to type in thread dot current thread which is referring to the main thread and we're going to use the dot set priority method so this is on a scale between 1 and 10 10 being the highest the highest priority and 1 being the lowest so if we changed this threads priority to 10 and then we get the priority it's going to display 10 so this main thread now has the highest priority and if we to one it now will have the lowest priority possible then and one other useful method with Reds is that we can check to see if a threat is currently alive so what we can do is use the is alive method so if we want to check to see if our main thread is alive we're gonna type in thread dot current thread dot is alive and then put this within a print line statement so system dot out dot print line and then we are again at saipin thread current thread is alive and it's going to check to see if our main thread is alive and it says it is true all right so one other thing that you can do with threads is that you can have a thread sleep so it's kind of like your program is paused so why don't we make some sort of counter and this will count down by seconds so what I'm going to do is create a for loop and we'll save int I and we'll start at 3 well count down from 3 and then we'll continue this for loop as long as I is greater than 0 and then let's decrement I by 1 each time and then what we're gonna do within this for loop is just display whatever I is and then we're going to type in thread dot sleep and then we can type in how many milliseconds we want this thread to sleep after each iteration of this for loop so let's have this pause for one second so it's kind of like we're counting down now you'll notice that this is underlined read it says unhandled exception type interrupted exception so you can surround this with a try and catch block or add it throws declaration at the top here and that error should go away and then once we exit this loop will just display a message such as you are done okay let's try this so normally when we have a for loop like this it iterates through these almost instantaneously but now if we had thread dot sleep for one second it's kind of like we're counting down by one second at a time then in one second intervals so that's how you can use the thread dot sleep method to have your thread or program sleep for a given amount of time so now what we're gonna do is actually create a second thread along with our main thread and there's one of two ways that we can create another thread I'll show you guys one way in this video and in the next video on multi-threading I'll show you guys another way so one way is that we can create a child class of the thread class so let's go to our project folder go to file new class and let's call this my thread and this my thread class will extends the thread class so my thread is going to inherit everything from the thread class there's actually this method called it run within the thread class and we're going to override that so my thread will do something else so it's going to be a public method that is void and it's called run and since we're overriding this we should probably add a note for good practice that says override because we're overriding this method so when run is executed let's just display that this thread is running okay so this is everything we need to do within our my thread class so let's create an instance of my thread so it's kind of like we're creating an object basically so we're going to type in the name of the class my thread and let's call this thread two equals new my thread and this will create a second thread along with our main thread that is currently running now with the second thread we can use some of these methods that we used for the first thread so let's check to see if this thread is currently alive so what we're going to type is the name of our thread so thread two dot is alive and we're going to put this within a print line statement so system dot out dot print line we're going to check to see if thread two is alive and this returns false and this is the reason why when you start a new thread or create a new thread you need to start it so the it begins basically so we're going to type in thread to dot start and this will start our thread so this will display that this thread is currently alive and also noticed that it executed our run function which just displays this thread is running so it can be tempting to put run here in place of start but if you were to check to see its status here it's going to say that this thread is currently not alive but it does execute our run function so if you want this thread to start you'll want to be sure to use the start function and not just the run function so that's how you can check to see if a thread is currently alive let's go over a few others let's check to see what this threads name is so we're going to type in the name of our thread so thread two dot get name and then put this within a print line statement so assisting that out dot print line thread to get name and the name of this thread is thread - 0 and remember with our main thread that was just named main so we can also change the name of this thread so I'm just going to copy this and before the print line statement we'll just type in thread 2 dot set name and let's change this to maybe second thread or whatever you want so if we were to display the name of our second thread thread 2 it's now named second thread now let's check thread two's parity so we'll type the name of this thread thread 2 dot get priority and then place this within a print line statement again system dot out dot print line thread to get priority and this has a priority of 5 which is the default basically now if you have one thread that creates another thread it's going to inherit the priority of the thread that created it so if we went back to our main thread and we set the priority to let's say the highest priority so this priority of thread too is now going to be 10 since it inherits the priority of the thread that created it which in this case would be main so we can also set the priority like we did here so that is the set priority at method so we're going to type in thread 2 dot set priority and let's set this to 1 so thread 2 now has a priority of 1 then compared to our main thread this by default has a priority of 5 so the main thread has a higher priority than our thread to thread basically so one other thing we can do is that we can check to see how many threads are currently active kind of like what we did with our first example so we're going to use the thread dot active count and I'm going to copy this and paste it and it's going to display how many threads are currently active and it says 1 that's because we need to start thread 2 and now the amount of threads that are currently active is 2 because we needed to start read to know we have one last thing to discuss there's two different kinds of threads they are user threads and daemon threads that sounds kind of cool right so a daemon thread is a low priority thread that runs in the background to perform tasks such as garbage collection and the Java Virtual Machine terminates itself when all user threads which are non daemon threads finish their execution so if we were to create another thread this would normally by default to be a user thread a non daemon --thread but we can actually change it so it is a daemon --thread first let's check to see if it is a demon thread so there's actually a method for this we type the name of the thread so thread 2 dot and we'll use the is daemon method and then we need to put this within a print line statement so assisting that out dot print line thread 2 dot is daemon and then we'll display this and run it so it says false so this thread is not a demon thread but we can actually set it so it is so we're going to type in thread to dot set demon true and run this again and it says true so this is a daemon --thread and it runs the program now we can actually change the my thread class and we can check to see if this is a daemon --thread so if this dot is demon will display this is a daemon --thread that is running otherwise else will just display system dot out dot print line this is a user thread that is running alright so if we were to revert this back to false it displays this is a user thread that is running and if we change this to a daemon --thread its going to display this is a daemon --thread that is running so like we said with daemon threads they are low priority threads that run in the background to perform tasks such as garbage collection and the java virtual machine will terminate when all user threads which are non daemon threads finished their execution so it's kind of like the java virtual machine doesn't care if there's daemon threads running the background it'll exit regardless as long as all user threads finish their execution so it's kind of like daemon threads are low priority kind of like what we did with setting the priority for our threads so that's the basics of threads it's kind of like a virtual CPU that has its own like set of instructions and we can have multiple threads running concurrently at the same time and another definition for this is multi-threading and that's something we'll cover in the next lesson so that's the basics of threads if you want a copy of all this code i'll post all of this in the comments down below but yeah that's the basics of threads in Java hey you yeah I'm talking to you if you learn something new then you can help me help you in three easy steps bye smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro [Music] you [Music]
Info
Channel: Bro Code
Views: 19,375
Rating: undefined out of 5
Keywords: Java threads, Java threads tutorial, Java Thread class, Java thread example, Java tutorial threads, Java threads tutorial for beginners, Java, thread, threads
Id: a_LBuCx1KTE
Channel Id: undefined
Length: 16min 0sec (960 seconds)
Published: Sun Jun 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.