ok so this is the last challenge and in
fact the last video in this section on
concurrency and threads and what I've done is
I've actually created a new class called
challenge
9 and actually
created new versions of the tutor and
student classes that are all in
all contained in this challenging 9
class and what I've done is use
synchronize blocks as well as a way to
notify all so go down and have a look at the code
you can see we're no longer
synchronizing the entire method and said
we're synchronizing just blocks in the
tutor study time as you can see here just a
block of code as well as the students .
hand in assignment or the new student in
this case and in assignment so we are synchronizing
just that block of code likewise
for new tutor we are doing the same thing as
well and we'll assume that the
synchronized blocks are the only
critical sections in our application
with that said we're definitely yeah we were
definitely over synchronizing the code
before so the new tutor
class
it's no longer sleeping is actually
called weight instead you can see on line
45 so when the student thread execute
the hand in assignment thread going to
notify the tutor thread...
...and tutor thread
will wake up and run so both
threads are actually acquiring the locks
in the same order first the tutor lock then
the student lock so everything looks
great at this point in time so actually
happens when we run this we run this
challenge 9 . main and you can see
we've still got a problem we still
deadlocked so can you figure out base on
this code now why that's still the
case so download if necessary the code
and study it for yourself and try to
figure out why its still deadlocked or
still deadlocking when we're running it and pause
the video while you're doing that and
then when you're ready to come back and
find out why start the video again and
will go through some reasons why
this is probably happening so pause the video now
and i'll see you when you come back
ok so welcome back lets discuss a
solution or find out more about what this
problem is all about one thing that this
example does
is demonstrates just how tricky it
can be to work with threads so let's
take a look at the sequence to see why
we're still deadlocked in this scenario
firstly the new tutor thread calls the
tutor . study time method and acquires
the new tutor lock and then the new student
lock now the thread reaches the
this . wait line of code that showed
you earlier and when it executes wait
it releases the new tutor lock and by the way
when I'm talking about
tutor and student here that's new tutor and
new student that's the new classes I
made in that challenge 9 source code and
refers them from this point on as tutor and students
just for simplicity anyway recall that
the wait and notify all methods must be
called from within synchronized blocks
to call wait on an object a thread
must be holding the objects lock when it
calls wait it releases the lock so the
student thread runs the hand in
assignment method and it gets the tutor lock
and calls get progress report and then
tries to get the student lock but tutor
thread is still holding it
and because of that the students thread
can't notify the tutor thread at this point we
obviously got a deadlock and that's
what was happening in the code
so the tricky part here is that it looks
like the two threads have to obtain the
locks in the same order from a casual
look at the code it doesn't seem
possible for a thread to get the
tutor lock and then block on getting
the student lock but because the wait
method results in a thread releasing the
lock until it's notified the student
thread can actually obtain a tutor lock
or the to the thread is still holding
the student lock
so we can add another item
to the what to check if the code is
deadlocking list now so take a look at
places where the code calls wait and
see if that may cause a deadlock now
the wait method results in a thread
releasing a lock before it is actually
exited the synchronized block and it's
premature albeit temporarily release can
lead to problems if we're not careful
so how do we go about fixing this well
that would really depend on the code
perhaps using reentrant lock objects
will work better
we could have the tutor thread release the
student lock before it calls wait and then
try and require it again once it's
woken up so there you go that's a
challenge 9 so hoped you enjoyed the
challenges that in fact this entire
section
this brings the concurrency and thread
section too a close so thanks for watching
as always and I'll see you in the next
section in the course