Java Essentials - Thread synchronization
9K views
Oct 24, 2024
Java Essentials - Thread synchronization
View Video Transcript
0:00
In this video we are discussing thread synchronization
0:04
Whenever multiple threads will be operating in our system, then they might be demanding
0:10
the same shareable resource. And it is a problem known as the critical section problem
0:16
So in case of critical section when the multiple threads are demanding the same shareable
0:20
resource, there is the same shareable database, same shareable file, same shareable, some other
0:26
I.O. resources. Then in that case, if we don't have to do that, we don't have to be able to be able to shareable, the same shareable
0:29
don't have any synchronization that which thread will be accessing which one earlier or later
0:35
then obviously some mal results may get produced so this thread synchronization is a very serious
0:41
problem so how to do this straight synchronization will be discussing that one in our section
0:48
today so synchronization means we are putting some ordering in the multi-threading environment
0:55
execution. So why we should synchronize threads? When we start two or more threads
1:02
within a program there may be a situation when multiple threads try to access the
1:08
same resource and finally they can produce an improper result due to concurrency issues So due to some not maintaining the synchronization between the threads it may produce some
1:21
incorrect result for us. So we can easily synchronize the tasks in Java by using the keyword or the using the block
1:30
that is known as the synchronized block. And every object in Java is associated with a concept that, that is known as the monitor
1:39
The monitor is used to control the access of the resources at a given point in time
1:46
So monitored is nothing but which will actually monitoring that how the resources will get allocated to the respective processes
1:54
So to have the better idea on this thread synchronization, let us go for one practical demonstration where it will be discussing this topic with one sample code
2:05
In this program, we are defining one class, the name of the class is transferred
2:10
it is having only one method that is a send message so this is a method we're having
2:15
so this method is a very simple method it is having one string to get printed and then
2:20
it will wait for one second delay for one second so that why we have put this 1 000 milliseconds as an input argument to this slip method which is public static void sleep long millies which throws interrupted exception we have discussed that one in the earlier
2:36
videos also that's why we put it's put it under the tri block and catch block is handling
2:41
this interruptive exception so e. print stack dress so the respective body for this catch block
2:48
is this one after after waiting for say one second of duration then this message is
2:54
sent message concatenation is sent this particular string will get printed now
3:00
here we're having one class the name of the class is message transfer which
3:04
extends thread so as it is extending thread so here we can write the
3:10
word the method that is run here message transfer is the respective constructor
3:15
it is having one private string message and transferred object that is a
3:19
transfer class object here you can find that here we have defined the transfer
3:23
transfer class so transfer class object message transfer message transfer is nothing but the constructor of this class that is the message transfer which
3:32
takes this string message and transfer object trans as input argument to
3:37
instantiate this instance variables within the class transfer message transfer and here as this particular class is extending thread class so that why we overriding the run method here and
3:50
here you have used the synchronized message transfer you can find that this
3:55
message transfer is nothing but the transfer class object and synchronous message
3:59
transferred within this synchronous block we have right we have written we have
4:03
called this method there is a message transfer dot send message that means
4:07
we have called this method under the synchronized block so what will happen when we will be having multiple objects
4:15
under this message transfer class and when for all of them will be calling the
4:20
start method okay let me discuss this one I think that will clear your conception
4:24
so public class sync thread so this is a class we have defined under this
4:28
we're having the main function and we have here we have defined one transfer class
4:33
object that is a transfer trans is a new transfer and message transfer objects
4:38
that means this class objects were defining so that we can we can call the start
4:43
we can call the start method which will call in turn the run method here so that's
4:49
why message transfer message one is the new message transfer what is this this is
4:53
the respective message and this is the transfer class object you can find that we
4:57
defined that respect
#Java (Programming Language)
#Programming
#Software