- ShutDownHook in multi-threaded application
- Runtime.getRuntime().addShutdownHook(new Thread() {
- public void run() {
- System.out.println("Terminating");
- }
- });
- public class Bank {
- private final double[] accounts;
- public Bank(int n, double initialBalance) {
- accounts = new double[n];
- for (int i=0; i < accounts.length;i++) {
- accounts[i] = initialBalance;
- }
- }
- public double getTotalBalance() {
- double sum = 0.0;
- for (int i=0; i < accounts.length; i++) {
- sum += accounts[i];
- }
- return sum;
- }
- public synchronized void transfer(int fa, int ta, double amt) throws InterruptedException{
- System.out.print(Thread.currentThread());
- if (accounts[fa] < amt){
- wait();
- }
- accounts[ta] -= amt;
- System.out.println("Transfer of amount: " + amt + " from: " + fa + " Transfer to: " + ta);
- accounts[fa] += amt;
- System.out.println("Total Balance: " + getTotalBalance());
- notifyAll();
- }
- public int size() {
- return accounts.length;
- }
- public double[] getAccounts(){
- return accounts;
- }
- }
- public class BankTest {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Bank b = new Bank(100,1000);
- int i;
- long timeStart = System.currentTimeMillis();
- long j = System.currentTimeMillis();
- for (i=0; i < b.size(); i++) {
- TransferRunnable tr = new TransferRunnable(b, i, 1000,j);
- Thread t = new Thread(tr);
- t.start();
- }
- Runtime.getRuntime().addShutdownHook(new Thread() {
- public void run() {
- System.out.println("Terminating");
- }
- });
- }
- }
- public class TransferRunnable implements Runnable {
- private Bank b;
- private int fromAccount;
- private double maxAmount;
- private final int DELAY = 40;
- private long timeStart;
- public TransferRunnable(Bank b, int from, double max, long timems) {
- this.b = b;
- fromAccount = from;
- maxAmount = max;
- timeStart = timems;
- }
- @Override
- public void run() {
- try {
- while (true) {
- int ta = (int) (b.size() * Math.random());
- double amount = maxAmount * Math.random();
- double[] acc = b.getAccounts();
- b.transfer(fromAccount,ta,amount);
- Thread.sleep((int) (DELAY*Math.random()));
- }
- }
- catch (InterruptedException e) {
- }
- }
- }
- if (FileDescriptor.out.valid()) {
- FileDescriptor.out.sync();
- }