ok so for challenge 8 what I've done
is I've pasted in some code here
and I've made it available in the
resources section of this video so you
can download it if needed and what
this code does what's actually got a
tutor and a student to class and lets
have a look we've got the tutor
here and the student
so the idea is that the tutor thread calls
the tutor. study time method and you can
see the study time method here in tutor and
in case you're wondering the threads are
actually started here we got a
thread here for tutor and a thread for
student so the tutor thread calls the
tutor.f study time method and it
waits for the student to arrive and hand
in assignment then the tutor and student
study together so the student thread the
we defined here you can see we're
starting here on line 22 and defining
on line 19 that call the tutor . in hand
assignment method
sorry the students hand in assignment
method so they're handing assignment
method actually calls a tutor. get
progress report so can you spot the
problem with this code and again just looking
at it then we can see that we got a
thread here tutor. study time is
called automatically so you can see it's
waiting for something to happen
student.start study is called after
the delay and the student thread is
calling its hand in assignment method
from the student class and you can see
that is calling the tutor. get progress
report so can you spot the problem with
this code so what is the problem if you
did find one call now this scenario
just to be clear doesn't have a
one-size-fits-all answer so we're not
going to code the solution but we're
going to think about at least one way to
fix it so the goal is to fix it if you can
at least try and come up with such a
scenario where you understand what the
problem is with the code and what that
problem is called so pause the
video here now and go away and see if you can
figure that out and when you're ready to
come back to see the solution start
the video again so pause the video and
I'll see you when you get back
alright let's talk about the solution
the problem was a deadlock let's look at
a potential sequence to understand
what's going on
firstly you saw that the tutor
thread called tutor .study time then the
tutor thread gets the lock for the tutor
object and that's because the study time
method is synchronized and it prints
tutor has arrived and wait for the
student which we simulated by calling
sleep now the student thread runs and
calls the hand in assignment and since the
method is synchronized it gets the lock
for the student object and it calls
get progress report but tutor
thread is holding the lock for the tutor
object so the student thread blocks
now the tutor thread wakes up and runs and
called student . start study but the
method is synchronized and the student
thread is holding a lock for the student
object so the tutor thread blocks and at
this point we've got a deadlock
that's because neither thread will ever
release the lock that the other thread is
waiting for
so essentially the threads are trying to
acquire the same set of locks but in a
different order and are often results in
a deadlock the tutors thread gets the
tutor lock and then wants the student
lock the student thread gets the student
lock and then wants the tutor lock
now the fix will depend
on the code now perhaps over synchronization is
occuring
do we really need to synchronize all the
methods or can we isolate critical
sections of code within the methods and
use synchronized blocks instead in a
real-world application we really would
have to study the code now we might also
consider rewriting some of the code to
do the tutor and student objects really
need to call each other in a circular
fashion and can we make these two
parameters in anyway
so another potential solution would be
to use reentrant lock objects and the
try lock method with a timeout value if
the try lock method times out the thread
wouldn't be able to execute the critical
section of code but at least the
application wouldn't be deadlocked also
a thread could release any locks it's
holding when the try lock times out
allowing any threads waiting for those
locks to run successfully
now they may will be other
solutions when dealing with a deadlock
situation look for the following firstly
is a set of locks been obtained in a
different order multiple threads and
that was the case in the code samples
showed in earlier in this video if so can we
force all threads to obtain the locks in
the same order
are we over synchronizing the code can
rewrite the code to break any circular
call patents and finally would using
reentrant lock objects help
that's it that's the solution as i
mentioned i'm not going to actually code
that but it should really give you a
good understanding of what you need to
do to solve the situation like this
I'm going to finish the video here
now in the next video we'll do the last
challenge challenge 9 and we'll see
you in that videos