How to terminate a thread in java?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi i'm a programmer and you're watching learn to code in this lecture we are going to learn about how to terminate a thread in java the most common practice to terminate a thread is by interrupting it now the question is how can we safely interrupt a thread right so java thread api provides method called interrupt so when we call this interrupt method on any thread then two things can happen if the thread is in sleep or weight state then it will throw interrupted exception so based upon the exception we can act upon it and the second condition is if the thread is running then calling an interruption it will make the interrupt flag to true and it will perform a normal execution the thread will not be interrupted but the java programmer can use the interrupt flag and stop the thread regarding the interrupt we have two more methods provided by java normal thread api first one is public static boolean interrupted method which is a static method and the another one is is interrupted it's a boolean method the static interrupted method returns the interrupted flag after that it sets the flag to false if it is true whereas is interrupted method returns the interrupted flag either true or false let's write some code to understand more about it so please be sure you subscribe to my channel as i upload new tutorials every week let's create a new class called reading terminate task and implements runnable interface i'm gonna overwrite run method as well so inside run method we will write the code of reading a text file and printing onto the console but little different from the previous examples so let me mention the location of the file so say its string file path name equals and let's go and copy paste the path so this is the path i'm going to copy and the file name is course topic dot txt okay now i'm gonna create an instance of a file file file equals new file and pass the file path name which is the location of the text file in here i'm going to create a new instance of a scanner class scanner read equals null and read equals new of scanner class and let's pass the file instance here it is showing exception so let's add a catch block okay it was expecting a file not found exception all right so here i'm gonna write the while loop and inside while loop i'm going to check a condition reader dot has next line if the scanner reader has a next line then we are going to print that line so let's write it is system.out.println and here i'm going to pass the thread name first so thread.current.getname [Music] okay let me copy this and put it outside the while loop [Music] the string current thread name equals um pass this okay now let's copy paste this current thread name and concatenate with the colon and then the reader dot next line which will read the line okay this is done so let's go back to the main driver class and create the instance of reading terminate task task 1 equals new reading terminate task and let's create a thread thread 1 equals new thread and i'm gonna pass task one and set the name of a thread inside constructor itself thread one now thread one dot start will invoke this thread and let's copy paste this and change the name of the task one reference and thread also okay let's change this and also the thread name inside the constructor here i'm gonna print the thread name before terminating the thread so let's say string current thread equals thread dot current thread dot get name and let's i'm gonna pass this into the system our dot print allen so current thread interrupting the thread and then the thread name thread one dot get name [Music] let's call the interrupt method on third one oh oops let's correct it thread one dot interrupt and let's copy paste these two lines and change the thread one to two and give some spaces here now i am going to put some sleep time before calling interrupt methods on both the instances so let's write the time unit dot millisecond dot sleep of 1000 milliseconds it is expecting a interrupted exception as we call the sleep method so let's throw exception on method signature all right now also let's put two thousand millisecond sleep time before thread two and we'll just change the 1000 to 2000 millisecond now let's go back to our task reading terminate task so inside the uh the while loop let's add a sleep after printing each line so that we can understand better the behavior of terminating a thread so let's add a time unit dot millisecond dot sleep or for let's say 300 milliseconds okay now let's surround it with the try catch and i'm gonna put one if condition if thread one dot equals the current thread then we are going to sleep for 300 milliseconds okay and else condition let's put it somewhat like time unit dot millisecond dot sleep let's say 200 milliseconds and also let's put an end line here when it access the run method so let me write system.out.println then test is end and so before exiting run method we will check the status of interrupt flag by using the interrupted method thread dot interrupted or let me put one more condition also while catching the interrupted exception i'm gonna set a new boolean volatile flag called interrupt and let's make true we haven't defined this flag let's do it private volatile boolean interrupt nice add the interrupt flag in the if condition so if the thread is interrupted by any other thread or the interrupt exception happens when it is in sleep or waiting state then what we are gonna do we will print the current thread is interrupted and let's break the loop when it is interrupted oh i wrote outside the while loop so let let's cut and paste it [Music] awesome it looks perfect now okay let me interchange the sleep time and why did i change this because i want to stop the thread too later that's it okay let's run it and see ourselves what would be our output okay nice now thread one and two has been started and end it through the interrupt okay so thread one and two are running at the beginning now here the main thread has interrupted the thread one so thread one is interrupted and ended as it came out of the run method we recognize by seeing this end line and thread two is still running and this is also being interrupted by the main thread thread two also ended here so now we have learned how to terminate a threat in java in the next lecture we will talk about how to handle uncaught exception in threads so see you there
Info
Channel: SkoonCode
Views: 3,475
Rating: undefined out of 5
Keywords: Terminate thread, interrupt thread, interrupt in java, how to terminate a thread, what is interrupt in threads, Java normal thread API interrupt, Java Terminate, java interrupt, java Threads functions
Id: DTN8fWhIcBA
Channel Id: undefined
Length: 10min 30sec (630 seconds)
Published: Sun Nov 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.