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?
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
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
List
model to DefaultListModel
JList.setModel(new DefaultListModel());
JList.getModel()
and cast it to DefaultListModel
Runnable
object with run()
method to add a new element
SwingUtilities.invokeLater()
with your
Runnable
object
DefaultListModel model = ...
String element = ...
Runnable addListElement = new Runnable() {
public void run() {
model.addElement(element);
}
};
SwingUtilities.invokeLater(addListElement);
SwingUtilities.invokeAndWait(Runnable)