CSE 132 (Spring 2015)
Studio 4: Multiple Locks and Dining Philosophers
(Locks and Lox)
Review studio procedures, if
necessary, before starting.
Some guidelines for this week's studio:
- Form groups of no more than four people. If your group
is joined by a fifth person, split into two groups.
- Be sure you commit your repository and that you are cleared by a TA before
you leave.
Get Acquainted (15 minutes)
- The software for this studio is organized into various packages by
functionality. This should make navigating through the code easier.
- Make sure the runAll() method in Main is calling
.run() on the new Threads, so that no concurrency
is happening yet.
- Run the Philosophers' problem by running the StudioMain as a
Java Application. It is in the studio4 package. Make sure
you understand what you see in terms of the philsophers' behavior.
-
Take a look at PhilViz and notice the correspondence between
event names and colors.
Spend a few minutes changing the colors that get shown when
a Phil gets hungry.
- Add another state in the perform() of a Phil, so that
it plays after studying. Pick a suitable color to
show when the Phil is playing in PhilViz.
-
Be sure to use status("playing") so that
its change in state is observed via the PropertyChangeSupport.
- If you have time and inclination, arrange for the Phil randomly to play
or study (but not do both) after it has eaten.
- OK time for deadlock: in StudioMain, change .run() in
runAll() to .start().
Recall in Studio 3 you used the debugger to help understand
the nature of deadlock:
- Which locks does a Thread currently own?
- For which locks is a Thread currently waiting?
Run StudioMain under the debugger, and use the steps from
Studio 3 to explain why
there is deadlock.
- After using the debugger to understand deadlock, explain what you see
to a TA.
Double Locks
The Phil object, when it performs, has to get two locks, one
on each Chop.
- Based on what you learned in class today,
you will develop a
DoubleLock object that initially just captures the
behavior of obtaining two locks but later is more clever and
avoids deadlock:
- The DoubleLock class implements LockInvokable.
- The constructor for DoubleLock
takes in two SingleLockInvokable objects,
say o1 and o2:
public DoubleLock(SingleLockInvokable o1, SingleLockInvokable o2) {
}
- Its lockInvoke(Runnable) method can start out as follows (you
need to put this code in there):
/**
* While holding both locks, run r
* (Code that used to be in Phil)
*/
public void lockInvoke(final Runnable r) {
lock1.lockInvoke(
new Runnable() {
public void run() {
sleep(50);
lock2.lockInvoke(r);
}
}
);
}
- Open Phil in the editor.
- In the constructor, note the DoubleLock object that is
constructed
based on the existing LockPubs l1 and l2.
- Change perform, as directed by the comments,
so that after being hungry, it
uses the DoubleLock's lockInvoke method to
run the eating Runnable.
- You should continue to see deadlock.
- Now modify DoubleLock so that deadlock is impossible,
using Havender's algorithm as discussed in class.
- LockPub is supposed to return a unique ID for an object
via getUniqueID(), but currently returns 0 -- fix that (see notes below).
- What role does SingleLockInvokable's getUniqueID() play in this algorithm?
- Why is hashCode() inadequate for ordering objects?
- Take a look at System.identityHashCode(Object)
- Show a TA your code and run the progam to show it works without
deadlock.
Multiple Locks (Puzzler)
If you are short on time, think about this one outside of Studio.
If two locks are good, then an arbitrary number might be better.
-
Look at the studio4.puzzler package.
The MultipleLock should implement LockInvokable,
as follows:
- The constructor accepts an array of Objects.
- The lockInvoke method obtains a lock on all of the
objects in the array, and then runs the Runnable.
- Don't worry about deadlock yet, just be sure you hold all the locks.
- Remember you can use helper methods if needed.
- Try out the MultLockTest as a JUnit test and
make sure it passes.
- OK, now worry about deadlock: how do you make sure that use of your
MultipleLock class never results in deadlock?
Demo
When you done with this studio, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
- If you request propagation, it does not happen immediately,
but should be posted in the next day or so