home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-04-23 | 5.0 KB | 194 lines |
- package COM.odi.demo.threads;
-
- /**
- * <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
- *
- * main program which starts two threads
- * One thread computes total number of children under 10 having amount more
- * than 10000 in the bank.
- * Second thread computes total number of poeple having more than $10000 and
- * total of such amount.
- *
- * The Database is created in the Load object and the name of the Database
- * is a command line parameter. Command line for this program is :
- * java -DtellerDB=g:\testDB\teller.odb COM.odi.demo.threads.Banking1
- * if the Database already exists with this name then the Database is not created.
- * The Roots in the Database start from Institution/Bank . Institution Object
- * contains a vector of Account holders which can be browsed through the
- * OSVector class.
- */
-
- import COM.odi.*;
- import java.util.Enumeration;
- import COM.odi.util.OSVector;
-
-
- public class Banking1 {
- /* Activities */
- /* Sum up children <= 10 and amount >=10000 in the account */
- public static final int CHILD = 1;
-
- /* compute number of Account holders having amount >= 10000 and sum of this
- amount.
- */
- public static final int ACT10 = 2;
-
- /* Database name */
- public static String dbName;
-
- public static void main(String args[]) {
-
- dbName = System.getProperty("tellerDB");
- ObjectStore.initialize(null, null);
-
- if (dbName == null)
- dbName = "teller.odb";
-
- /* populate Database */
-
- new Load(dbName);
-
- Database db = Database.open(dbName, ObjectStore.OPEN_READONLY);
- Transaction t = Transaction.begin(ObjectStore.READONLY);
- Compute t1 = new Compute("Sum", "Bank1", Thread.currentThread(), CHILD);
- Compute t2 = new Compute("Children", "Bank1", Thread.currentThread(), ACT10);
-
- /* start threads */
-
- t1.start();
- t2.start();
-
- /* wait for completion of all threads then commit the transaction */
-
- try {
- t1.join();
- t2.join();
- } catch (InterruptedException e1) { }
- t.commit();
- db.close();
- ObjectStore.shutdown(true);
- System.exit(0);
- }
-
- };
-
- /* the thread class */
-
- class Compute extends Thread {
- /* name of the thread */
-
- private String name;
-
- private Institution inst;
-
- /* name of the bank same as database root name */
-
- private String bankName;
-
- /* ObjectStore.initialize will be attached to this thread */
-
- private Thread mainThread;
-
- /* Database Object */
-
- private Database db;
-
- /* action to be taken in this thread */
-
- private int action;
-
- /**
- * Initialize the Compute object before starting the thread.
- * @param name The name of the thread.
- * @param bankName Name of the bank/database Root object to access.
- * @mainThread pointer to the parent thread.
- */
- public Compute(String name, String bankName, Thread mainThread, int action) {
- super(name);
- this.name = name;
- this.mainThread = mainThread;
- this.bankName = bankName;
- this.action = action;
- }
-
- /**
- * The thread starts and ends here.
- */
- public void run() {
-
- init();
-
- /* depending on name the Thread performs different activity */
-
- if (action == Banking1.ACT10) {
- accountsMoreThan10k();
- } else if (action == Banking1.CHILD) {
- childrenUnder10AndMoreThan10k();
- }
- System.out.println("END THREAD :" + name);
- }
-
- /**
- * Thread related initialization.
- */
- private void init() {
- try {
- /* attach ObjectStore initialization to main thread */
- ObjectStore.initialize(mainThread);
- db = Database.open(Banking1.dbName, ObjectStore.OPEN_READONLY);
- } catch (ObjectStoreException e) {
- System.out.println(e.getMessage());
- stop();
- }
- }
-
- /**
- * Find total number of accounts with more than 10K in their accounts,
- * sum this amount and display each account maching the criteria
- */
- private void accountsMoreThan10k() {
- int numberOfAccounts = 0;
- float totalAmount = 0F;
- float amt;
-
- inst = (Institution)db.getRoot(bankName);
-
- /* get the Account to be read from the institution */
- Enumeration e = inst.getAccountEnum();
- Account act;
- while( e.hasMoreElements()) {
- act = (Account)e.nextElement();
- if((amt = act.getBalAmount()) >= 10000) {
- totalAmount += amt;
- numberOfAccounts += 1;
- act.displayAccountInfo(name);
- }
- }
- System.out.println("Number Of Accounts having More than $10K " + numberOfAccounts);
- System.out.println("Sum of amount in Accounts having More than $10K " + totalAmount);
- }
-
- /**
- * Find total number of accounts with more than 10K in their accounts
- * and age under 10 and display each account maching the criteria.
- */
- private void childrenUnder10AndMoreThan10k() {
- int numberOfChildren = 0;
-
- inst = (Institution)db.getRoot(bankName);
-
- Enumeration e = inst.getAccountEnum();
- Account act;
- while( e.hasMoreElements()) {
- act = (Account)e.nextElement();
- if((act.getAge() < 10) && (act.getBalAmount() >= 10000)) {
- act.displayAccountInfo(name);
- numberOfChildren += 1;
- }
- }
- System.out.println("Number Of children having More than $10K and age less than 10 " + numberOfChildren);
- }
-
- }
-
-