class Bank1
{
int amount=10000;
synchronized void withdraw(int amt)
{
System.out.println("Available balance in the account is : "+amount);
System.out.println("Going to withdraw rs 15000...");
if(this.amount<amt)
{
System.out.println("Less balance, waiting for deposit...");
try
{
wait();
}
catch(Exception e){}
}
this.amount-=amt;
System.out.println("Withdraw completed...");
System.out.println("Now balance in the account is : "+amount);
}
synchronized void deposit(int amt)
{
System.out.println("Going to deposit rs 10000...");
this.amount+=amt;
System.out.println("Deposit completed");
System.out.println("Now balance in the account is : "+amount);
notify();
}
}
class ThreadCommunication1
{
public static void main(String surat[])
{
final Bank1 c=new Bank1();
new Thread()
{
public void run()
{
c.withdraw(15000);
}
}.start();
new Thread()
{
public void run()
{
c.deposit(10000);
}
}.start();
}
}
Output:
{
int amount=10000;
synchronized void withdraw(int amt)
{
System.out.println("Available balance in the account is : "+amount);
System.out.println("Going to withdraw rs 15000...");
if(this.amount<amt)
{
System.out.println("Less balance, waiting for deposit...");
try
{
wait();
}
catch(Exception e){}
}
this.amount-=amt;
System.out.println("Withdraw completed...");
System.out.println("Now balance in the account is : "+amount);
}
synchronized void deposit(int amt)
{
System.out.println("Going to deposit rs 10000...");
this.amount+=amt;
System.out.println("Deposit completed");
System.out.println("Now balance in the account is : "+amount);
notify();
}
}
class ThreadCommunication1
{
public static void main(String surat[])
{
final Bank1 c=new Bank1();
new Thread()
{
public void run()
{
c.withdraw(15000);
}
}.start();
new Thread()
{
public void run()
{
c.deposit(10000);
}
}.start();
}
}
Output:
No comments:
Post a Comment