Wednesday, July 23, 2014

Deadlock in multithreading - Java





public class Account  {
 double balance;
 int id;
 Account(){

 }
 Account(double _balance,int _id){
 balance=_balance;
 id=_id;
 }
 void withdraw(double amount){
    balance -= amount;
 }

 void deposit(double amount){
    balance += amount;
 }

  void transfer(Account from, Account to, double amount){
   
       synchronized (from) {
           synchronized (to) {
            from.withdraw(amount);
          to.deposit(amount);
           }
         }
       
           
/*  from.withdraw(amount);
          to.deposit(amount); */    
   }

}

No comments:

Post a Comment