ok so let's set a few more methods to
the bank account class we're going to
need those for the next challenge we're
going to add one method that gets the
bank account number and a second method
that prints the bank account number
i'm going to start at the bottom of the
class...
...
alright so let's move on to the
challenge and see what that's all about
alright so challenge three is to make
the bank account class thread-safe again
using the synchronized keyword so we added
those two new methods that you saw me
add you want to update the code so that
the bank account class is thread-safe
and actually use the synchronized
keyword when necessary so that's a
challenge
pause the video now and go away and
complete the challenge and when you're
ready to come back and see what I've
come up with for the solution you can
play the video again so pause the video
and I'll see you when you get back okay
so here's the solution to challenge
three
and sorry to tell you that was a bit of
a trick challenge we'd actually don't
have to make any changes to the code and
that's because both threads only read the
account number so therefore thread
interference isn't an issue now it would
actually be a mistake to synchronize the
get account number and print account number
methods in this scenario and that's
because would be over synchronizing the
code and applications with a large
number of threads that can have a
noticeably negative impact on
performance sorry for the trick but it's
important to understand where and when
not to synchronize code this was a
situation where we didn't need to
actually synchronize anything else
ok so let's move on to the next
challenge challenge 4
so challenge 4 now is to use a
re-entrant lock so instead of using the
synchronized keyword that we've done so
far in the bank account class we want to
use the reentrant lock to make the bank
account class thread safe so that your
challenges is write implement the
reentrant lock class into the bank
account class so have go with that challenge
and i'll see you when you get back
so pause the video now
alright challenge 4 let's talk about
the solution before we go into writing
some code
so we need to create a lock
object and to use the lock method in the
withdraw and deposits methods 2 things
to keep in mind here
firstly both threads need to compete for
the same lock so we create only one lock
object that we use in all the methods
that have critical sections of code
secondly want to put the critical
section of code within a try finally
block to ensure that the lock will be
released no matter what and to set aside
don't forget to create the lock instance
in the bank account constructor
alright so on to actually implementing this
in code let's go back to IntelliJ
alright so firstly to the
bank account class and let's go and add
a field for our lock to the class so we're
going to create a private lock....
....
...
....
...
....
...
so that's actually it
so we've converted that rentrant
lock now and we've initialized the
renteran lock in the constructor obviously
we added the the field on line fourteen
as well to be able to do that if you
recall we wanted to compete for the same
lock which is why we've created that in
the bank account class and we are
initializing it to the bank account
constructor then were using the lock
. lock in both the deposit and
withdraw methods and having a try-catch
or not so much a catch but a
try finally so that we can guarantee the
lock is unlocked and then we're just
putting the critical section of code
within the try to either add the amount
or deduct the amount based on whether
we're calling the deposit or withdrawal
method so that's it that's challenge 4
completed so let's move on now to
challenge 5
ok so challenge 5 is to use a try lock
with a timeout value instead of using
the lock method which we just seen i'm
going to use try lock or I am asking you to
implement try lock with a timeout value of
one second
now if the waiting period times out we want
to print the message could not get the
lock to the console and it's a hint here
use the form of the try lock method
that accepts two parameters the first
parameter is the timeout value and the
second parameter is the time unit of the
first parameter and I suggest you use
time-out . milliseconds for that
second parameter alright so go ahead and
have a go at implementing try lock in this
code and pause the video now and i'll
see you when you get back alright so
onto the solution for challenge five so
the only real wrinkle here with using
the try lock method is that it can
throw an interruptedexception so we also
have to handle that and also if you
think about it we're going to call
unlock only if the try lock call
returns true and the thread gets the
lock so the try lock method
times out then the method will return
false and the thread won't get the lock
so obviously in that case we don't want
to try and unlock it and also that's
scenario its going to execute the
else code that we're going to put in
there instead and you might be tempted
to put the finally block after the catch
for interruptedexception or rearrange
the try-catch block in some other way
that's problematic but I suggest you
don't do it that way but you do it the
way that I'm going to show you now
alright so let's have a look at the
deposit record first so again it's
probably quicker if I just delete the
method since its
so small rather than trying to sort of
adapt it first we need that outer try
so....
...
...and the reason
we're catching that is the try lock
actually catches or throws can
potentially throw a
interruptedexception so we need to
handle that i'm not going to actually do
anything in there i'm just going to do
something here you would normally handle
that in the case have been
interruptedexception
the next to use the try lock we're going to
put.....
....
...
....so let's just
go through and type the same thing in for
withdraw is going to be identical other
than the fact we deducting the amount and
not adding it but it's good practice to
type this again just to go through it
again again we're setting up a try with
a catch of the....
interruptedexception and the reason
we're doing that is the try lock which
were about to type in actually throws
interruptedexception....
....
...
....
...
....
...
....
alright so that's challenge 5 solution
completed so let's end the video here
now in the next video we'll move on that
and have a look at challenge six so
we'll see you next video