Hi there if you don't understand that conker and see how much better resource utilization which in turn 2
helps in improving that throughput. 3
But at the beginning of the chapter we also say that concurrent programming is more complex compared 4
to sequential programming. 5
So now let's understand that aspect of concurrent programming. 6
That is why it is complex compared to sequential programming. 7
While it is nice to learn about this advance stuff related to multi-threaded programming we should also 8
be aware of certain safety hazards. 9
There are certain concurrency hazards and this isn't the goal to get a concurrency has called a risk 10
condition. 11
The main complexity of multithreaded programming comes when the same object is shared across multiple 12
threads and that object has some speed that is that data which can be modified by the text. 13
Let's look at an example here let's consider these two users. 14
John and Anita on their side they are a happily married couple. 15
Here's the example scenario is that they have a joint bank account and let's see how that balance off 16
only a hundred dollars. 17
Now when customers use this banking application dig it up isn't it by looks like we see here and there 18
is also a task called bank account. 19
Since it is a task it implements that are noble edifice on the bank can cross has one instance variable 20
called balance and also one instance method called Make withdrawn which is invoked when the user is 21
making it with done the make with document that simply checks if there is enough balance for withdrawn 22
. 23
And if there is only then it will be that the withdrawal amount from the balance amount on would then 24
allow the user to withdraw the amount. 25
So this balance check is very critical so that there is no onward with of money. 26
Now when the user accesses the banking application then a bank account object will be created for that 27
to users. 28
It can and will be passed us input with a third that represents that user non-theism that book John 29
on it happened to use the banking application at the same time. 30
Then since they represent the Simek on only a single bank account object will be created for them as 31
does input to the threats that are present. 32
John. 33
Anita just like the BBC here. 34
No let's consider that. 35
But John I don't want to get draw an amount of dollar I got on the same time. 36
That is they are using the banking system at the same time to get created on Lettice's should jewler 37
bakes Johnston at first. 38
And so Johnston that would invoke the make withdrawal method under the make with Dolemite that the balance 39
is checked. 40
And since John only wants 75 bucks the check will succeed. 41
However there is room that wretched Guler decides to send John Buddha on noble estate before the new 42
balances computer. 43
So Jon Stewart is an honorable straight on tourch a jeweler will then bring an industrial to running 44
state Undun understood it will and does make withdrawal method and real check if there is enough balance 45
. 46
Since John did not withdraw any money the balance cheque will succeed and the new balances computed 47
which will be dollar fine. 48
Next John will be brought to running's do it as unattested it has completed execution and assuming that 49
the balance is still $100 John will order all. 50
Which brings the balance to minus $50. 51
So the system is in a bad state now so that data got corrupted and this all happened because somebody 52
was not careful enough when quoting this class. 53
That is a bank account object is not safe and what it means is that the bank account object had stayed 54
that is mutable. 55
That is the instance variable balance is mutable and the bank account object was being shared between 56
threads which were accessing a method that can change the mutable state changing the mutable state is 57
fine but the process of changing the state was not properly managed due to which the bank account object 58
was not to when in not properly managed. 59
What it means is that was a particular concurrency hasn't called risk race condition so the make withdrawn 60
method hazardous condition under the type of race condition it exemplifies is called as check. 61
Then act which is a very common type of race condition under just got his check done act as we are first 62
making a check if there is enough balance. 63
Only then we are performing some action which is to compute the new balance. 64
The problem is the desirable outcome which is not overdraw and that is dependent on the wretched behavior 65
. 66
In this case after Jones thread observes the balance it is suspended briefly so that Anita's thread 67
finishes to completion undaunted. 68
Earlier observation is no longer valid so the correctness of the program depends on luck. 69
If Jon Stewart is not suspended and is allowed to compute the new balance right after performing the 70
badness check then everything is good. 71
OK so life is good. 72
So the solution is that make the document that should be made to run as one single economic unit that 73
is once a threat and does make withdrawal. 74
It must be a lot to finish before any other threat can and does make withdrawing matter and this behavior 75
is also referred to as a mutual exclusion. 76
You must have heard of it that is only one threat is being able to enter the method at a given instance 77
of by and the next lesson we will look at locking which is a job loss but it didn't mechanism for ensuring 78
atomic behavior. 79
So non-threat if object means an incorrect program and an incorrect program can lead to Dudack option 80
sometimes programs that are not threats they might work on pro forma for years but they may fail at 81
any moment. 82
So they are like timebombs. 83
So basically we are relying on luck but not that the problem is mainly because of how mutable state 84
that can be shared. 85
But if the object does not have any state at all then object destructive. 86
That is if it does not have any instance variables then it means that there is no state in the first 87
place and program would have and the methods that operate on local variables. 88
No let's look at another interesting example that there is a race condition here. 89
The ID generator class is responsible for generating unique IDs in this system. 90
So IDs are long variable and the method get new ID which simply written that next ID unfought that all 91
it is doing is incrementing the current ID value. 92
Now we would assume that this increment operation is a single action because it is only a single statement 93
unlike our make withdrawal example. 94
However that is not the case. 95
And there are three discrete operations going on in the background. 96
The first operation is to first the current value of ID then increment it. 97
And finally we write the new value back. 98
So due to the fact that this single statement has multiple operations we have a risk condition. 99
For instance let's add the current value of ID is to. 100
Then let's say there is a threat a which invokes this method get your ID so it could read the current 101
value of ID then it performs the increment operation. 102
But before that writes back the new value for Treacher you look and suspended and bring and under threat 103
etc.. 104
Be in good running state which would involve this method and treat the value for ID as the previous 105
threat threat. 106
He did not write it back. 107
Subtract B or increment it and write the value back. 108
So now the ideal value is for next right. 109
It would raise you on it would continue by performing only the last operation because that's when it 110
got suspended or no it would write the value back which is also 4. 111
So essentially ID was incremented only once. 112
Although the method did you ID was in work twice. 113
So one of the updates was lost. 114
This method would work perfectly in a single threaded environment but in a multi-threaded environment 115
it runs the risk of working incorrectly. 116
So once again we need to ensure that the increment operations statement executes as a single economic 117
statement under that see how that can be done. 118
And the next lesson not have to be fully eclipsed and it cooked them all offer this condition. 119
This is a quick demonstration of the John an example which we have seen earlier. 120
So it's going to be the exact same example and how a bank account and both John and thought well how 121
a joint account. 122
And they have a balance off $200 and they're going to withdraw an amount of dollars 75 at around that 123
same time. 124
OK. 125
So that was the scenario on we are going to simulate that exact same scenario and we will also see how 126
it can generate the undesirable effect or undesirable outcome off overdrawing. 127
Okay. 128
So here the name of the class the new class is race can be more on your husband I get into the concurrency 129
package and here in the statement we are creating the bank account object. 130
So it's a class which implements the Runnable interface. 131
So it does that to us. 132
And we are setting the balance of $100 on here we are creating two threads John and Anita and we are 133
passing the same bank account object to both threats. 134
Okay. 135
And we're also setting the names here forward that aspect of threats John and Anita on we are stopping 136
them here and here we have the bank account object bank account class which implements runnable. 137
So it has the pirate incidents but it will balance here as a certain matter. 138
And here is that run matter on that on. 139
We are invoking the make with documented with the withdrawal amount which is $75. 140
So here is the make Woodhall method. 141
And initially we are making that balance check and if it succeeds then we computed our new balance and 142
we have a bunch of brand statements saying the contract that is executing the method. 143
Now the balance is less than zero. 144
Then the money overdrawn text will be Pinkard. 145
OK. 146
So that's just not a desirable outcome but it can happen because off the race condition. 147
OK. 148
So let's go ahead on this. 149
So here it is. 150
So money has been overdrawn. 151
Okay. 152
And this is what happened. 153
So initially John has entered the make with Dortmunder and this statement is getting printed which means 154
that John is about to withdraw. 155
After that John has been sent into runnable state and I need to also under this method and unadvised 156
has also on it also sees that the balance is greater than or equal to the amount which means that there 157
is enough balance. 158
So the next statement is about or with guys getting printed and after that and it is going into into 159
runnable straight I end up most likely John is in the running state and is computing the balance of 160
New Balance amount and B happened in the statement John has withdrawn 75 bucks. 161
Okay. 162
Then Anita makes up and John is being sent into unbelted and Anita is also drawing 75 bucks. 163
OK. 164
So but this time with the new balance it is actually it goes in negative okay and it goes into negative 165
balance as minus 50 now. 166
So after that it looks like John is coming back into running state and it is being sent back into into 167
run up Alstad. 168
Okay after the husband drunk. 169
So because of that here when John finishes the execution of make withdrawal history sees that balance 170
is resident zero and access money. 171
All are wrong. 172
OK. 173
On John Street finishes its execution after that on this dusted it is going to resume its execution 174
unflat for heart rate also the balances less than zero and this thing is getting printed twice. 175
OK. 176
So although we see the money overdrawn message to be printed twice in reality the money has been overdrawn 177
only once and that is based on the. 178
OK. 179
So it was fine then. 180
Jon Stewart has withdrawn the money. 181
OK. 182
It was not overdrawn. 183
It is only when it's us that has withdrawn. 184
Then the money has been overdrawn. 185
OK it's just because of the interleaving of that threat we are seeing this Caernarfon output. 186
So there we can see how the threat should you get us behaving. 187
So it is interleaving both the threats and we have a risk condition and we can see that undesirable 188
outcome. 189
So it's because this particular class is not thread safe. 190
So we need to make it safe and that is the subject of the next lesson. 191
So this god is available in the resources section and that's about it. 192
NC in the next lesson. 193
Thank you. 194
And happy coding.