Java Essentials - Multithreading by reunnable interface
11K views
Oct 24, 2024
Java Essentials - Multithreading by reunnable interface
View Video Transcript
0:00
Multi-threading by runable interface
0:04
So in the previous video we have discussed multi-threading by extending the thread class
0:09
So whenever we are trying to implement one multi-threading environment, we are having two options
0:15
We can either extend the respective thread class or we can implement the runable interface
0:23
In the runable interface, we are having only one method to implement and that is the run
0:28
So whenever we are going to have the multi-threading environment, implementing this runable interface, we are bound to define the run method
0:39
So let us go for further discussion. So how to use the runable interface
0:45
Without creating a subclass of the thread class, we can also create a thread by implementing the runable interface
0:53
For this case also, we need to redefine the run method. and write the code for the thread
1:01
So we know that run method is that very method within which you are writing the code
1:05
which is the task of the thread to be executed. After creating the class
1:11
we need to define the object of the thread class and in the constructor
1:15
and we need to pass our created class and use the start method to run the thread So this is the basic concept if I want to have the multi environment using the runable interface
1:30
So let us go for one practical demonstration on this concept and that will clear your all doubts
1:36
In this program we are going to define one third class implementing runable interface
1:43
So you know that in case of runable interface we're having one unimplemented method that is a run
1:48
So if you write one class, say, if you go for the implements runable and then if you put this opening and closing
2:04
braces you see here we are having on option that is the add unimplemented
2:09
method otherwise I can make this class abstract here so if you go for this
2:13
add an implement implemented method option you can find that this a run
2:17
method it is supposed to get implemented so similarly we did the same here so here
2:25
our class name is my thread class which implements runable and which is
2:29
overriding this run method we are writing the body for this run method and
2:33
within this body I will be ranging from 0 to 9 and we are going to print this
2:39
thread dot current thread dot get ID and the respective value of I so in this way
2:44
the respective run function body we have defined here so the trade its respective get ID with return the ID of the thread and that will get printed so within the class that
2:58
the trade with runable so this the name of the class is thread with runable
3:04
so here we're defining the main function we're defining this trade class
3:09
objects thread one and thread two and they are getting instantiated using this
3:14
my thread class objects actually we are creating the thread object with the my thread class object so in
3:22
this way you have defined this thread one and thread two so now if we call this
3:25
start and fun methods from this thread one and from the thread two to
3:30
thread class objects then this run function will be executed and within this
3:35
main function we're having the fall loop where I will be ranging from 0 to 9
3:39
and main thread colon line colon I will get printed so let us go for the
3:45
execution for the better understanding you can find that here this main thread line number 0 1 2 3 4 5 6 7 8 9 and then
3:56
for this particular thread ID it is getting printed as a 10 and 11 for the 2
4:02
objects that is a thread 1 and 3 so you can find that thread number 11 for this we
4:08
are having this line 0 11 line 1 11 line 2 11 line 3 but having this 11 line 9 here so respective i value is ranging from 0 to 9 similarly for thread number 10 we having the 0 we having this 1 and similarly
4:24
we're having this thread number 10 there is the ID it is getting printed and line
4:28
number 9 that means I here also the eye is ranging from 0 to 9 so just look at this
4:33
outcome look at this output the sequence in which it has got generated so that
4:38
means we are finding that this thread one dot method is actually calling this
4:43
run function and this trade two dot start this particular function calling is
4:47
actually calling the run function and also the main function all these three
4:51
functions are in execution in parallel so that's why you are finding that all
4:56
these lines are getting printed one after another in a mixed up way so if I
5:01
execute my program once again you can find that the sequence of this
5:04
execution and this line printing will be changed you can find that you see it has got printed in some different sequence if I go for execution
5:12
again it has got printed in some different sequence you see different times
5:17
different sequences are coming so that will depend upon the execution synchronization
5:22
in which order all these three that is a main thread and two threads that is
5:28
our object's names are respective thread one and three two how they are
5:32
executing within this machine and that's why the outcome is changing along with the execution
5:41
Thanks for watching this video
#Computer Education
#Java (Programming Language)