Hi there in this lesson let's see how we can avoid the race condition which we saw in the previous lesson 2
. 3
It was because the class was not correct. 4
If this is the make with dogmatic and visit that this method needs to execute as a single economic unit 5
in order to avoid the undesirable outcome of a drawing. 6
And for that we can use the jealous berichten locking mechanism called synchronization on going off 7
and going to the agent is to protect critical data which is shared and also mutable like the balance 8
instance variable in our example. 9
In fact we also how items 5:54 from Effective Java which is synchronize access to shared data and synchronization 10
ensures data protection by dis allowing threats from interfering with each other when we're talking 11
about synchronization we are talking about a block of code which needs to be synchronized on this block 12
of code. 13
Can one or more statements on a synchronized block basically contains parts. 14
One is a lock which acts like a real lock and we will look at that and the other is a block of code 15
which needs to be guarded by this lock on this block of code would be the critical data that needs to 16
be protected like the checking of the balance and the subsequent New Balance computation in our bank 17
account example and that lock here is nothing but a reference to an object. 18
And here is how a synchronized block looks like we will have the synchronized keyword followed by an 19
object reference in the parenthesis which acts as a lock and it is followed by a block of code and Caly 20
basis. 21
And here is an example. 22
Here we have them a third go which includes a synchronized block. 23
The lock is simply that this reference so reference to current object is serving as the lock on inside 24
the synchronized block. 25
We have the critical stuff on outside. 26
We have the non-critical stuff. 27
And let's see how locks work. 28
Always keep in mind that every object has a single lock and every thread and during a synchronized block 29
will automatically acquire the lock and will release it either upon exiting the synchronized block or 30
if an exception is generated within the synchronized block. 31
An important thing here to note is that at most one threat can why the objects lock. 32
As mentioned earlier an object has only a single lock on that block cannot be shared across the decks 33
. 34
This way only one thread gets to manipulate the critical data. 35
So if a thread wants to enter a synchronized block but the lock is unavailable which means that some 36
other thread has already acquired it. 37
In this case the thread goes into the blocked state. 38
So the template for the lock to be released by the current threat. 39
And once that lock is available again it put it quite it and then would enter the synchronized block 40
. 41
Look at more than one third could be waiting for the lock. 42
In that case once the lock is available only one of them will get to it an it ular will decide who that 43
lucky protests and this building locks associated with objects are called as intrinsic or mounted deadlocks 44
. 45
So that's the thing every object has a lock that needs to be acquired before entering gringas synchronized 46
block which protects critical data. 47
This way we cannot have race conditions as only one thread gets to use the entire synchronized block 48
next to synchronize method. 49
It is simply a shorthand for a synchronized block and we simply use the key word synchronized in the 50
method declaration and with that the matter is guarded by a lock on here the lock is simply the object 51
on which the method is invoked. 52
At this Bangkit called object does not have a race condition and this direct. 53
As mentioned earlier every object has a single lock so locks are but objects are not Marmelade. 54
So what it means is that if one threat has acquired an object's lock then no other thread Ginter any 55
of the objects synchronized methods here the lock would have been acquired by entering a synchronized 56
block or by entering this method. 57
And it is important to remember at this particular point. 58
And let's also do a bit of shooting here. 59
It will be one of the quiz questions too. 60
So keep that in mind and you may even be asked about it in an interview. 61
And here is another quick question on a potential question to a third going under any unsynchronized 62
method of the object. 63
Even if Glock is already acquired by some other threat so not a strict sions with unsynchronized amateurs 64
as they are do not build with critical data. 65
And here is an interesting one. 66
It's tactics and Grise methods use class object as a lock. 67
So every class has a single lock on every object also has a single lock Soviet using synchronization 68
to coordinate access to shared variables shit notably variables but there are a couple of important 69
things we need to know. 70
When it comes to coordinating access to shared variables crusting is that we need to use synchronization 71
everywhere. 72
The is just one instance that's going to get a bank account plus we have the make with Dolemite that 73
which is synchronized. 74
Now let's see if you also have this get automatic get balance between it and the balance around you 75
. 76
Now let's say John's track is inside. 77
Make the document that trying to $.75. 78
Let's say there is enough balance on the new balance of dollar 25 if it's competitor. 79
Let's also say that after the New Balances computer but before John Street exits to make with dogmatic 80
on Anita outstretched accesses get balanced about that which is not synchronized in this case. 81
There is a chance that it thread will still get a value of dollar hundred rather than the new balance 82
off dollar 25. 83
It means you dollar. 84
Only if I do but there is no guarantee. 85
So basically in a multi-threaded program if it dragged right so gladly you into a variable. 86
There is no guarantee that the renewables new value will be visible to another thread trying to read 87
that variable's value and it has been kept that way mainly for performance results as such into strict 88
coordination can harm the performance. 89
We may discuss why it is that way but for now it is good enough to know that it's got something to do 90
with romance but if we want to get into here that it does today we'll see the most recent value of balance 91
that maybe are better. 92
But don't sell it then we need to synchronize the get balance method as well as synchronized. 93
Any other place where balance variable is used and this is also discussed in item 5:54 which was mentioned 94
earlier. 95
So synchronization means not only just mutual exclusion but also reliable communication between threats 96
. 97
And we may get back to this in a follow up lesson well there are some alternative ways to also achieve 98
reliable communication. 99
Now the second important thing to note about coordinating access to shared multiple with is that always 100
use the same lock when guarding the CMS shared multiple variables. 101
That is do not guard a variable using one lock at one place and using some other lock at some other 102
place. 103
And here is an example. 104
So once again it is a bank account example. 105
And we have the synchronized make with dogmatics and we also know how that method called deposit which 106
actually uses a synchronized block that is guarding the same balance variable as some deposit is being 107
added. 108
Now the synchronized method as we know use us bank account objects block that is the object on which 109
the method has been invoked. 110
Hollywood the deposit method is using a string object reference as the lock on that should not be done 111
. 112
So two different clocks are being used. 113
You got the same variable. 114
And so the effect of synchronization is lost. 115
That is John an ostrich can be using synchronous stuff concurrently for instance lets say the bank account 116
has only 25 bucks and John wants to withdraw a dollar seventy 75. 117
So let's as you John request on it to transfer 75 bucks from her personal account to this joint account 118
and that's all you got in a hurry on both axes of the system at around the same time and let's say on 119
it AUSTRAC gets into the deposit met that and also into the synchronized block. 120
But before the deposit is added sugar switches to Jon Stewart and Jon Stewart will execute make with 121
donga method to completion and we find that there isn't enough balance and once Jon Stewart exits make 122
that Dawn unattested that will come back into running state and their deposit. 123
So John is still unable to withdraw the money just because the court was using different clocks. 124
Maybe not a great example. 125
But we shouldn't be doing this. 126
Also normally an object's intrinsic clock is used to guard the shared mutable state. 127
So that's about it. 128
In less than 30 seconds. 129
Let's also go ahead and make our make with Dolemite. 130
Synchronized so that we can get rid of the race condition. 131
OK here it is the bank account example and we know that the bank account object was not active. 132
So let's just go ahead and make the make that automatic synchronized. 133
Now we are good. 134
I mean if we execute we can see that that is no longer the interleaving between the tracks. 135
So John has withdrawn 75 bucks and when it dies trying to withdraw is not enough balance. 136
OK. 137
So that's synchronization. 138
And in the previous season we also Lubet another classical idea generator which was generating unique 139
IDs and it hard to get new ID and it could return the same ID on two different locations because the 140
object was not turret's. 141
So once again if we make unsynchronized it would work fine. 142
So that's about it that's about synchronization. 143
Thank you. 144
I'm happy Cody