alright so just before we start
challenge six let's actually make a
change to both the deposit and withdraw
methods we're going to add a status
variable to both methods we're going to
initialize it to false and if the thread
gets the lock we're going to set the
status variable to true and at the end
of each method we're going to print the
status to the console so let's just go
ahead and make those changes which
shouldn't take too long
we're going to introduce a local
variable boolean status...
....
...
....
...
so that's it so we would do the same here
for the withdraw method....
....
...
....
...
....
...
....
alright so let's go ahead now and look
at challenge six
so challenge six is to update the
code so that the status variable is
thread-safe use whatever method you like
the synchronized keyword or the lock
object so the idea again is to make sure
that the status variable is thread-safe
so go ahead and try that now pause
the video and I'll see you when you get
back and i'll show you the solution
pause the video now
alright so let's talk about the solution
to challenge six
and sorry to tell you that was
a trick challenge since the status
variable is a local variable it's
already thread-safe if you recall we
learned in this section of the course
that local variables are stored on
the thread stack so each thread will
have its own copy threads won't
interfere with each other when it comes
to setting and getting the status value
in this case so you didn't need to make
any changes to the source code
alright let's move on to the next
challenge alright so for this challenge
challenge challenge 7 this is a spot
and fix the problem so what I'm going to
do is put some more or paste
some code into this project and get you to
look at it and the see if you can come up with
a fix
so the code i'm going to place is
available in the resources section but
we need to create a new class i'm going to
create that and call it challenge 7
for a lack of better term we're going to paste in
the code
ok so there's the code and i'll just go
over to a slide briefly and just tell you
what is comprised of this was comprised
of what's in this class and then
ultimately what you need to do
alright so this challenge challenge 7
spot and fix the problem
so we're going to take a look at a
similar application which also deals
with bank accounts but in this case it
performs performs transfers from one
bank account to another
now this version of the bank account
class which I called new bank account
has three methods deposit withdraw and
transfer now the deposit and withdraw
methods are very similar to what we had
before the transfer method first tries
to withdraw the money from the source
account if it's successful and it
deposits the money into the destination
account and returns true and if it's not
successful then it's going to refund the
source account and return false
now in the main method we create two
bank accounts and start two threads each
thread will perform a transfer using an
instance of the transfer class the
transfer classes run method loops until
the transfer is successful
so the challenge is to spot what's wrong
with this code and to fix it so what do
we call this particular situation that's
in the code deadlock live locking etc
why is it happening is another question
for you to answer and also how would we
fix it and as a hint the quickest way
to get an inkling of what's wrong is to
type or copy/paste the code that I'm about to
show you again into a new intellij
project and to run it so let's get back to
the code before we start talking about
solutions just to go over the code again
alright so here's the code that i pasted
it so you can see that we were
creating two new bank objects
based on this class which have created
as a class in this file and it's
a little bit different we've got the
withdrawal methods and the deposit
methods which are very similar and you
can see that were using we are still using
the try lock method and also got this
new one here this transfer we've got
a destination class we go through and we
look at transferring the money and
there's also this class transfer as well
so you can study the source code and I
suggest you do study it to see if you can
come up the answers to what's wrong with
it and how to go about fixing it and in
fact what the situation is so pause the
video now and see if you can get that
working when we come back we'll go
through the solution
pause the video
so the code actually gives rise to a
live lock situation the two threads aren't
blocked but they can't progress so
they'll keep withdrawing money trying to
do the transfer failing and then
refunding the money then they'll loop
back and try to do the transfer again
until we manually terminate the
application so to fix it what we need to
do is figure out
firstly why it's happening and then
proceed but in this case the codes never
releasing the lock so therefore the
first thread that gets the lock for a
bank account hangs onto that lock
forever and the second thread can then
never get the lock for the destination
account
so here is one potential sequence of
execution
firstly the thread data transfer one
calls the transfer method to transfer
money from the account 1 to account 2
transfer one thread gets account one
lock when the transfer method calls the
withdrawal method and withdraws the
money to be transferred from the account
one transfer 2 thread called
transfer transfer method to transfer
money from account 2 to account one the
transferred 2 thread gets account the
account 2 lock and withdraws the
money to be transferred from account 2
transfer one is thread for
transfer one calls account 2 deposit
method but transferred 2 at this point is
holding the lock so the transfer fails
so it refund the money back to account
one which is still holding the lock and
then loops back
then transferred 2 the transfer 2
thread calls account 2 deposit method
but transfer one the thread is holding
the lock so the transfer fails so it
refund their money back to account 2
which is still holding the lock and then
loops back now since the transfer one
and the transfer two threads never
release the locks they're holding they
keep looping until we terminate the
application so the fix in this case is
to have the threads release the lock by
calling the unlock method and remember
that we always want to use a tri finally
block when dealing with lock objects
once we implement the code changes when
we run it the transfers will eventually
be successful and the application will
terminate normally so lets now go and make the
code changes in IntelliJ alright so
we're going to start off now we're going
to go to the deposit and withdraw
methods noting we're looking at them
we've got if in here if lock . try lock and
that's putting the code into go ahead
and do the balance transfers of it
successfully gets a lock but what its not
doing is we haven't got any code in here
to unlock anything we never unlock the
lock at the end so what we really need
to do is under the try lock introduce
another try
outside here so try and put all this
code that was actually in the triblock
paste that into the inner triblock then
the outer one needs to have a finally
it's going to be....
it'll be all we need to do so we are
guaranteeing that the lock is going to
be unlocked
once we've completed the processing and
again we are only trying to unlock if we
manage to acquire lock which we have the
code is being executed within the if
block this code here
alright so let's do the same for the
deposit method so we're going to
add a try block and add it in the if
block and get the existing try block
code
copy all of that into the inner triblock
and the outer one is just going to
finally....so that
literally should be all we need to do so
just to confirm this
and given that I actually got to some
code in here in the main method for
challenge 7 and firstly run this to make
sure i'm running the right class and run
challenge 7 . main and we should find
that the code works now we can see
that the transfer withdrew the money we
have a situation at one point where the
destination account was busy and
refunded money but it went through and
did it again and eventually got through
and everything completed and work
successfully you can see that transfer
one withdrew ten dollars and deposited
$10 likewise for the first calls here
withdrew ten dollars and you can see
the second section here withdrew 55 dollars
and deposited the 55 so we've got two
completed here transfer 2 transfer 1
we're both completed so that is
challenge 7 completed alright so I'm
going to finish the video here now in
the next video we still got some
challenges to do so lets work on
challenge eight in that video so I'll
see you in the next video