JAVA CODE FOR SYNCHRONIZATION-BANK | URDINESH

Software Programming, Tutorials, Interview Preparations,Stock Market,BSE/NSE, General informations

Tuesday, May 20, 2014

JAVA CODE FOR SYNCHRONIZATION-BANK



PROGRAM:

class Account
{
int acno;
float amt;
Account(int a,float a1)
{
acno=a;
amt=a1;
System.out.println("\nNow,the Balance is:Rs."+amt);
}
synchronized void putamt(float a)
{
System.out.println("\nProcess::Depositing Amount");
System.out.println("Depositing in Account is :Rs."+a);
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println("Exception is:"+e.toString());
}
amt+=a;
System.out.println("After Deposit,the Amount is:Rs."+amt);
}
synchronized float getamt(float a)
{
System.out.println("\nProcess::Withdrawing Amount");
System.out.println("Amount to be Withdraw is:Rs."+a);
if(a>amt)
{
System.out.println("\nDear Customer,Not Enough Money!!,Please Check your Balance.");
return 0;
}
else
{
amt-=a;
System.out.println("After Withdrawal,the Amount is:Rs."+amt);
return(a);
}

}
}
class deposit extends Thread
{
Account o;
deposit(Account t)
{
o=t;
}
public void run()
{
o.putamt(8000.00f);
}
}
class  withdraw extends Thread
{
Account b;
withdraw(Account t)
{
b=t;
}
public void run()
{
float r=b.getamt(500f);
            }
}
class Bank
{
public static void main(String arg[])
{
Account a=new Account(100,500.00f);
deposit d=new deposit(a);
withdraw w=new withdraw(a);
d.start();
w.start();
}
 }








OUTPUT:


Now,the Balance is:Rs.1500.0

Process::Depositing Amount
Depositing in Account is :Rs.10000.0
After Deposit,the Amount is:Rs.11500.0

Process::Withdrawing Amount
Amount to be Withdraw is:Rs.1500.0
After Withdrawal,the Amount is:Rs.10000.0

No comments:

Post a Comment

Thanks for your valuable comments

Followers