so we've covered quite a bit in this
section so let's actually practice what
we've learned
now if you want to type in and run
this code bear in mind that I when
working with threads as was shown in
this section continually timing will
affect what you see so you may have to
run a few times to see the problems
we're discussing but we're gonna be
doing first is working with this bank
account class and this code is available
for you to copy and paste in or you can
just type it in and follow along
and obviously being a bank account this
code is really simplified a real world
bank account class will have many more
methods and the deposit and withdraw
methods would also be a lot more complex
but we want to focus on concurrency so
we are actually keeping it simple
alright so onto the challenge the first
quite a few challenges
coming up
so the challenge 1 is to create and start
threads now we could have two people
using a joint bank account at the same
time you want to create and start 2
threads that use the same bank
account instance and an initial balance
of a thousand dollars now one will
deposit 300 into the bank account and
then withdraw 50 the others going to
deposit 203.75 dollars and then
withdraw 100 so that's the challenge and
it's going to be bit of a start in code
just to get you going
as you can see on the screen there so
that's it go away and see if you can
solve that challenge and when you're
ready to come back and see what we've
done start the video so at this point
pause the video and complete the
challenge and i'll see you when you get
back
ok so how did you get on hopefully you
manage to solve the challenge so let's
talk about to how we would actually
solve this challenge now just to be
clear I've created this new project
concurrency challenges and I've got the
files here I've got the starter code
that i talked about in the slides and
also the bank account class and again
these resources are available for you to
download in the resources section of
this video
alright so what do we need to do well
there are two ways we can create and
start the threads
the first is to extend the thread class
and implement the run method it wouldn't
make sense to have the bank account
class extend thread because each
transaction on a bank account will be
different if we want to create the
threads by extending thread we use
anonymous thread classes
let's go and have a go at that can be
putting this code in the main class in
the main method here we're going to type....
...
...and we are getting a warning saying
that the variable account is accessed
from within the inner class needs to be
declared final so let's do that make it
a final....
....
...
alright so that's the first one and the
second thread tr thread 2
...
alright so that's the two threads and we
need to start them
so...
....so we can run that and we are
not getting any output because we
haven't actually put any output in
there but that's basically the way to go
about doing it if you wanted to you
could maybe you've actually done this
you actually printed out balance just
to make sure that it's actually
depositing and withdrawing the right
amounts so that's the first way of doing
it now the second ways to create and
start threads to use the runnable
interface now once again it doesn't make
sense to have bank account the bank
account class implements runnable going
to use anonymous runnable instances I'll
just comment this out now up until
there because we're going to leave the
last two lines they're going to start
with....
....
...
....
...
and that would be the second way of
doing it creating a runnable so let's
challenge one let's work on another
challenge now go ahead and let's look at
the second challenge
the second challenge is to make the bank
account class thread-safe using the
synchronize keyword so i hope you can
see looking at that code that we had the
bank account class that there is going
to be thread interference when two
threads are accessing the same bank
account instance at the same time so we
have to make the bank account class
thread-safe that's our next challenge so
using the synchronized keyword to make
the bank account class thread safe is
your challenge so have a go at doing
that pause the video now and when
you're ready to see my solution come
back and we'll go through it pause the
video
so making the bank account thread safe by
using the synchronized keywords so how are we
going to go about that what we need to
do first is going to close down the
run window we need to open a bank
account class because that's where we're
going to be making the changes and what
we want to do is synchronized any method
that is updating a field so we got the
deposit and withdraw methods so they're
the obvious candidates to put the
synchronized code in so there are two ways
of doing this the first way is just to
change say the method definition is
public synchronized...
...
simple that synchronizing the entire
method like so that's synchronizing
the entire methods but an alternative
solution would be would be to
synchronize the code within the methods
using this so I'm going to do is just to
be consistent i'm going to take a copy
of that paste it and I'll just comment
out so that you got that in the
original source code you need to
download that let's put this back to
what it was not putting synchronized in
the method anymore but actually
synchronizing with in so we can actually
create a synchronized block so
synchronized and we synchronize on this
being the current object and we put the
field instance variable in there likewise
for the second one synchronized
this again and put the code in there
the code is actually changing the
instance variable field so both ways
are equivalent for this code but if
the deposit and withdraw methods
contained more code and not all the code
was a critical section then the second
solution which I've got on the screen
now that's not commented out
synchronizing on this in other words you
can see on line is 25 and 31 would be a
better solution
remember that generally speaking we want
to synchronize the smallest amount of
code possible to minimize the
performance impact on the application
so we only want to synchronize an entire
method when all the code within the
method is a critical section so that's
the solution to the second challenge
going to stop the video here with the
next video we'll move on to challenge
three see you in the next video