1. Suppose two users want to withdraw some money from same bank account. They went to two different ATM counters and checked the available balance. Finally they have withdrawn some money and again checked the available balance. While one user is withdrawing money another user can not check the availability of balance or cannot execute his/her transaction. Design this scenario by implementing the concept of synchronization in such ways so that inconsistencies never happen and each of them always can see correct account balance information


import java.util.*;
import java.util.concurrent.Semaphore;


public class first
{
    public static void main(String[] args)
    {
         Scanner vinnu = new Scanner(System.in);
        Semaphore semaphore = new Semaphore(1);    
        bank counter = new bank(500, semaphore);
        new Person("I", counter, semaphore);    
        new Person("II", counter, semaphore);    
       
    }
}


class bank {

    int balance;
    Semaphore semaphore;

    bank(int amount, Semaphore semaphore)
    {
        this.balance = amount;
        this.semaphore = semaphore;
    }
     public synchronized void withDrawingBalance(int money, String person) throws InterruptedException {
        semaphore.acquire();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            System.out.println(e);
        }
        if (balance >= money) {
            balance -= money;
            System.out.println(" ");
            System.out.println("-->>Person" + person + " amount withdrawal completed");
            System.out.println(" ");
        } else {
            System.out.println(" ");
            System.out.println("-->>Person" + person + " Not sufficient balance");
            System.out.println(" ");
        }
        System.out.println("");
        semaphore.release();
    }

    public synchronized void checkingBalance(String person) throws InterruptedException {
        semaphore.acquire();
        System.out.println(" ");
        System.out.println("-->>person " + person + " checking  balance");
        System.out.println(" ");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            System.out.println(e);
        }
        System.out.println(" ");
        System.out.println("-->>person " + person + " available balance = " + balance);
        System.out.println("");
        semaphore.release();
    }

 
}

class Person implements Runnable {

    String person;
    Thread t;
    bank counter;
    Semaphore semaphore;

    Person(String id, bank ba, Semaphore semaphore) {
        this.person = id;
        this.counter = ba;
        this.semaphore = semaphore;
        t = new Thread(this);
        t.start();
    }

   
    public void run() {
        try {
              System.out.println(" ");
              System.out.println("Welcome to the account "+person);
                System.out.println(" ");
                 System.out.println(" "+person+" has entered the atm ");
                   System.out.println(" ");
            counter.checkingBalance(person);
            Thread.sleep(1000);
            int amount1 = 300,amount2= 200;
            System.out.println(" ");        
            System.out.println("-->>Person" + person + " wants to withdraw money "+amount1);
            if (semaphore.availablePermits() != 0) {                      //checking balance
                counter.withDrawingBalance(amount1, person);                //checking balance
            } else {                                                          //withdraw failed
                System.out.println("-->>person " + person + " withdrawal  failed");
                  System.out.println(" ");
            }
           
            Thread.sleep(1000);
            System.out.println("-->>person " + person + " wants to withdraw money "+amount2);
              System.out.println(" ");
            if (semaphore.availablePermits() != 0) {                                          //checking balance
                counter.withDrawingBalance(amount2, person);                                   //checking balance
            } else {                                                                            //withdraw failed
                System.out.println("-->>Person " + person + " withdrawal  failed");
                  System.out.println(" ");
            }
               Thread.sleep(1000);
           
            counter.checkingBalance(person);
        } catch (InterruptedException e) {
            System.out.println(e);
        }
    }
}

1. Suppose two users want to withdraw some money from same bank account. They went to two different ATM counters and checked the available balance. Finally they have withdrawn some money and again checked the available balance. While one user is withdrawing money another user can not check the availability of balance or cannot execute his/her transaction. Design this scenario by implementing the concept of synchronization in such ways so that inconsistencies never happen and each of them always can see correct account balance information 1. Suppose two users want to withdraw some money from same bank account. They went to two different ATM counters and checked the available balance. Finally they have withdrawn some money and again checked the available balance. While one user is withdrawing money another user can not check the availability of balance or cannot execute his/her transaction. Design this scenario by implementing the concept of synchronization in such ways so that inconsistencies never happen and each of them always can see correct account balance information Reviewed by Unknown on 03:14 Rating: 5

No comments:

Powered by Blogger.