Tutorial week 2

  1. A lockfile is a file that is created to show that a process has control of a resource. Other processes trying to access that resource are expected to test to see if the file exists: if it does, then some other process has control of the resource, otherwise creation of the lockfile asserts control of the resource
    1. What operations must be collected together as atomic for this to work?
    2. What happens if the controlling process crashes and is unable to remove the lockfile?
    3. How can other processes gain control of a locked resource if the controlling process has crashed?
    4. Can lockfiles be implemented in Java?
  2. A bank account can be represented by a file where the filename is the acount name, and the contents of the file (an integer) is the balance in the account. Write an account class with methods deposit() and withdraw() which are protected by a lockfile. That is when an attempt is made to deposit or withdraw, it will only succeed if there is no lockfile set, and sets one itself during the transaction.

    Write a bank class that will initialise a number of accounts and then randomly transfer money between them. What will you need to do to ensure that deadlock does not occur?

  3. Listing files in a directory is not guaranteed to be sorted into any order. This can be done by adding entries into a TreeSet. Adding strings to a TreeSet will sort them alphabetically. Modify the last directory-listing program given in the lecture to produce an alphabetic list of the files in the directory and its subdirectories. Note that TreeSet is not thread-safe and will need a synchronized method to access it
  4. (Only for those who know Swing - and really want to know about Swing and threads!). The Swing library is not threadsafe, but it uses multiple threads itself. Even if you write what looks like a sequential program, if you use Swing components then it is multithreaded. If you create a Jlist using a constructor to set the element list - no problem. But if you want to build up a list of items, adding to the list dynamically, then you have to
    1. Set the List model to DefaultListModel
      	JList.setModel(new DefaultListModel());
    2. When needed, get the model out of the list by JList.getModel() and cast it to DefaultListModel
    3. Create a new Runnable object with run() method to add a new element
    4. Call SwingUtilities.invokeLater() with your Runnable object
      
      	    DefaultListModel model = ...
      	    String element = ...
      	    Runnable addListElement = new Runnable() {
      	        public void run() {
      	            model.addElement(element);
      	        }
      	    };
      	    SwingUtilities.invokeLater(addListElement);
       
    5. If you want to do e.g. alphabetic inserts, then you have to make sure all pending events in the queue are processed, so use SwingUtilities.invokeAndWait(Runnable)
    So...
    1. Modify the directory-listing program of the lecture to insert items in alphabetic order

Jan Newmarch (http://jan.newmarch.name)
jan@newmarch.name
Last modified: Mon Jul 26 14:22:07 EST 2004
Copyright ©Jan Newmarch