CSE 132 (Spring 2010)
Review studio procedures before
Studio 3: Madeoff Bank Heist and a KWIC GUI
Some guidelines for this week's studio:
- Form groups of no more than three people. If your group
is joined by a fourth person, split into two groups of two.
- Try to pair with somebody who attended this morning's optional lecture.
- Be sure you are signed in with a TA and that you are cleared before
- Use two computers for this studio, with one open to the studio web page and one
for the eclipse workspace.
- Load the eclipse workspace using the same directions as you used for
studio 1, but use studio3 as the prefix to your
- The code from class should appear in the lecture package of
In this studio, you will find a
madeoff package, which contains a version of the
Account object you saw in CSE131.
As described in class, the code in this package is not
Most code is by design, in fact, not thread safe. In this case, the lack
of thread safety poses a problem for the bank.
- Follow the steps below to cause the requisite havoc.
- Ask questions and think about what is happening throughout this
part of the studio.
- In the end, you will understand why the code as a race condition and
what you can do to ensure the correct operation of the classes.
- Take a look at the Account object in the madeoff
- Remind yourself how the methods work:
- boolean withdraw(int)
- boolean transfer(int, Account)
Do not change the Account class until told to do so, but
do convince yourselves that it works properly.
- The Account's sleep() method simulates the passing of time
Thread.sleep(int), which causes the thread executing
the method to pause for the specified number of milliseconds.
Such passing of time would happen in the real world on a busy system. We need it
here to create the kind of environment in which real applications run.
- In the Bank class,
you will see a line of code in the runTransactions method:
transactions[i] = null; /* FIXME */
Replace the null with an instance of an anonymous
subclass of Thread that overrides its
Review your notes from today's lecture and see the
extra resources (also posted on the
the course calendar):
- In the run() method of the anonymous inner class you created,
write the following statement:
- It is likely that eclipse will show you a problem with your code at this
- The yellow lightbulb can help you out, but...
- It is important that you understand what you need to do to fix this.
- Take a look at
- Ask others around you or a TA to explain what's wrong.
- Fix this problem by making the appropriate variable final.
- This part of the studio is set up so that
NUMACCOUNTS Account instances are created, each
with INITIALBALANCE funds in them.
The runTransactions() method executes NUMTRANSACTIONS
each transferring TRANSFERAMOUNT funds from
one account to another. The particular accounts are chosen at random. It is
possible that the transfer will take place from and into the same account, which
- When the Bank functions correctly and performs
only transfers, the
following should be true:
The sum of all of the Accounts' balances should be
NUMACCOUNTS × INITIALBALANCE
- Go ahead and run Bank as a Java Application.
- It's not really over until the Done message is printed (this
may take about 15 seconds).
- What result do you see?
- Note that for each Thread you create, the code I supplied
calls its run method. What does that method do with respect
- Change the line
What is the difference between
- Run Bank again as a Java Application.
- Qualitatively, how does the program's response time change when
you use .start()? Why is it different?
- In terms of the output, what do you see? Try running the application
many times and describe the behavior.
- What you should be seeing is the bad behavior of a race condition,
as multiple threads compete for access to the various accounts' balances.
- Based on what you learned in class, try to put synchronized
on methods in Account to fix this problem:
- Most people will first try putting synchronized on
all the methods. Try that, and describe what you see.
- If you get tired of waiting, you can terminate the program by clicking
on the red-square stop button.
- It is important to understand why you see what you see. Look
at your notes from class about
- Try to construct a scenario
by hand for this program that shows the deadlock.
- Turn this scenario in on paper
as part of the
- Try putting synchronized only on boolean transfer(int, Account) since
that's the only method we really call. What happens and why?
OK so where should synchronization be placed, and why?
Proceed only if you have time and inclination. You're welcome to try the
KWIC GUI part out on your own or later in the semester.
KWIC GUI (time permitting)
As a group, experiment with netbeans to design a GUI for the KWIC lab we did last week.
How should you do this? Design is not something we can do by rote or by
formula. This kind of activity requires creativity and a willingness
to take risks. Put some crazy ideas out there and brainstorm them as
As a point of reference, I wrote a (let's go with: "straightforward")
GUI just to give you an idea of what can be done straightforwardly.
I know you can do better.
- Click here to have that experience.
- What swing elements do you see?
- Critical analysis is an important skill in
this course. As a group, come up with at least 3 points of critique on
my design. Be honest and succinct in your
critque, with the goal of giving me feedback
to improve my design.
OK your turn to design a GUI for KWIC. Some notes:
- Think about how you want to make the API of
KWIC available via the GUI:
- Display the list of all Words
- Allow a user to select a Word to display all
with the Word
- Drop the association between a Word and a Phrase
- Add an association between a Word and a Phrase
- Delete a Word
- Delete a Phrase
- You should browse through the swing online guides:
- You do not need to implement the GUI today, but try to design its
- Run netbeans (Programming...NetBeans 6.5.1)
and follow Jonathon Lundy's
(starting at II if you're using CEC computers). When you specify the
existing eclipse project, use the one that contains your kwic package
from Lab 2a, even if it is not yet done.
Submitting your work (read carefully)
- Complete the feedback.txt file and be sure to list
your group members' names and ID last-3-digits.
- Answer the other questions in the feedback.txt file.
- You must commit all of your work to your repository. It's best to do this
from the top-most level of your repository, which bears your studio name.
- You must have the TA clear you by signing your studio group sheet.
Be sure your names and IDs are written
clearly on the TA's demo sheet.
Last modified 08:59:27 CDT 03 June 2010
by Ron K. Cytron