Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 20th, 2012  |  syntax: None  |  size: 2.65 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ShutDownHook in multi-threaded application
  2. Runtime.getRuntime().addShutdownHook(new Thread() {
  3.             public void run() {
  4.                              System.out.println("Terminating");
  5.                         }
  6.         });
  7.        
  8. public class Bank {
  9.  private final double[] accounts;
  10.    public Bank(int n, double initialBalance) {
  11.     accounts = new double[n];
  12.     for (int i=0; i < accounts.length;i++) {
  13.         accounts[i] = initialBalance;
  14.     }
  15. }
  16.     public double getTotalBalance() {
  17.         double sum = 0.0;
  18.         for (int i=0; i < accounts.length; i++) {
  19.             sum += accounts[i];
  20.         }
  21.         return sum;
  22.     }
  23.     public synchronized void transfer(int fa, int ta, double amt) throws InterruptedException{
  24.         System.out.print(Thread.currentThread());
  25.         if (accounts[fa] < amt){
  26.                         wait();
  27.                     }
  28.         accounts[ta] -= amt;
  29.         System.out.println("Transfer of amount: " + amt + " from: " + fa + " Transfer to: " + ta);
  30.         accounts[fa] += amt;
  31.         System.out.println("Total Balance: " + getTotalBalance());
  32.         notifyAll();
  33.  
  34. }
  35. public int size() {
  36.     return accounts.length;
  37. }
  38. public double[] getAccounts(){
  39.     return accounts;
  40. }
  41.  
  42. }
  43.        
  44. public class BankTest {
  45.  
  46.     /**
  47.      * @param args the command line arguments
  48.      */
  49.     public static void main(String[] args) {
  50.             Bank b = new Bank(100,1000);
  51.     int i;
  52.         long timeStart = System.currentTimeMillis();
  53.         long j = System.currentTimeMillis();
  54.  
  55.  
  56.  
  57.  
  58.  
  59.             for (i=0; i < b.size(); i++) {
  60.         TransferRunnable tr = new TransferRunnable(b, i, 1000,j);
  61.         Thread t = new Thread(tr);
  62.         t.start();
  63.  
  64.     }
  65.            Runtime.getRuntime().addShutdownHook(new Thread() {
  66.         public void run() {
  67.                          System.out.println("Terminating");
  68.                     }
  69.     });
  70.  
  71.  
  72.         }
  73.  
  74.  
  75.     }
  76.        
  77. public class TransferRunnable implements Runnable {
  78. private Bank b;
  79. private int fromAccount;
  80. private double maxAmount;
  81. private final int DELAY = 40;
  82. private long timeStart;
  83. public TransferRunnable(Bank b, int from, double max, long timems) {
  84.     this.b = b;
  85.     fromAccount = from;
  86.     maxAmount = max;
  87.         timeStart = timems;
  88. }
  89. @Override
  90. public void run() {
  91.  
  92.         try {
  93.         while (true) {
  94.             int ta = (int) (b.size() * Math.random());
  95.             double amount =  maxAmount * Math.random();
  96.                     double[] acc = b.getAccounts();
  97.                     b.transfer(fromAccount,ta,amount);
  98.             Thread.sleep((int) (DELAY*Math.random()));
  99.         }
  100.     }
  101.         catch (InterruptedException e) {
  102.  
  103.         }
  104.  
  105.     }
  106.  
  107. }
  108.        
  109. if (FileDescriptor.out.valid()) {
  110.     FileDescriptor.out.sync();
  111. }