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