CSE 132 (Spring 2009)
Studio 3: Madeoff Bank Heist and a KWIC GUI

Review studio procedures, if necessary, before starting.

Some guidelines for this week's studio:


  1. Open eclipse and your CSE132 project, or create a new Java project CSE132, asking it to keep source and object files in the same folder (ask a student for help if you need it).
  2. Download and import this zip into your project.

Madeoff Bank

In the zip you installed for 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 thread safe. Most code is, in fact, not thread safe, but in this case the lack of thread safety poses a problem for the bank.

  1. Take a look at the Account object in the madeoff package.
  2. Remind yourself how the methods work:
    Do not change the Account class until told to do so, but do convince yourselves that it works properly.
  3. The Account's sleep() method simulates the passing of time by calling Thread.sleep(int), which causes the thread executing the method to pause for the specified number of milliseconds.
  4. 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 run() method.
    Review your notes from today's lecture and see the extra resources (also posted on the the course calendar):
  5. In the run() method of the anonymous inner class you created, write the following statement:
    acct1.transfer(TRANSFERAMOUNT, acct2);
  6. It is likely that eclipse will show you a problem with your code at this point.

  7. 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 transactions, 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 is fine.

  8. When the Bank functions correctly and performs only transfers, the following should be true:
    The sum of all of the Accounts' balances should be

  9. Go ahead and run Bank as a Java Application.
    • 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 to threads?
  10. Change the line
    What is the difference between .run() and .start()?
  11. Run Bank again as a Java Application.
  12. Qualitatively, how does the program's response time change when you use .start()? Why is it different?
  13. In terms of the output, what do you see? Try running the application many times and describe the behavior.
  14. What you should be seeing is the bad behavior of a race condition, as multiple threads compete for access to the various accounts' balances.
  15. Based on what you learned in class, try to put synchronized on methods in Account to fix this problem:
    1. 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 deadlock.
      • Try to construct a scenario by hand for this program that shows the deadlock.
      • Turn this scenario in on paper as part of the studio.
    2. 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?

KWIC GUI (time permitting)

As a group, you will design and prototype a GUI for the KWIC lab we did last week. On Thursday during lab, you will integrate your ideas to produce a GUI-driven KWIC.

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 a group.

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.

OK your turn to design a GUI for KWIC. Some notes:

Last modified 20:11:00 CST 11 January 2010 by Ron K. Cytron