Program : To demonstrate the concept of synchronization.
Algorithm
Step 1: Start.
Step 2: Create class bank.
Step 3: Boolean recvd=false,int bal=600,int amt=0,int wamt=0.
Step 4: Create class d implements Runnable.
Step 5: Create class w implements Runnable.
Step 6: new d(b).
Step 7: new w(b).
Step 8: Stop.
Function synchronized void withdraw()
Related Articles
Step 1: Start.
Step 2: if(!recvd) go to 3 else to 4..
Step 3: wait().
Step 4: Read wamt.
Step 5: if(wamt>bal) go to 6 else to 7..
Step 6: Print “Not enough money in your account-your current balance is”,bal
goto 11.
Step 7: if(wamt>1&&wamt1&&amt2000) go to 11 else to 13.
Step 11: recvd=true.
Step 12: notify().
Step 13: Exit.
Constructor d(bank b)
Step 1: Start.
Step 2: this.b=b..
Step 3: new Thread(this,”deposit”).start().
Step 4: Exit.
Function public void run() of class d
Step 1: Start.
Step 2: Repeat step 3 until true.
Step 3: b.deposit().
Step 4: Exit.
Constructor w(bank b)
Step 1: Start.
Step 2: this.b=b..
Step 3: new Thread(this,”withdraw”).start().
Step 4: Exit.
Function public void run() of class w
Step 1: Start.
Step 2: Repeat step 3 until true.
Step 3: b.withdraw().
Step 4: Exit.
Program
import java.io.*;
import java.lang.*;
class Bank
{
String msg;
boolean recvd=false;
int bal;
int amt;
int wamt;
Bank()
{
bal=600;
amt=0;
wamt=0;
}
synchronized void withdraw()
{
if(!recvd)
try
{
wait();
}
catch(Exception e)
{
System.out.println("Error : " + e );
}
try
{
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader b=new BufferedReader(in);
System.out.println("Type withdrawal amount");
wamt=Integer.parseInt(b.readLine());
if(wamt>bal)
{
System.out.println("Not enough money in your A/c:-Your current balance is "+bal);
}
else
{
if(wamt>1&&wamt1&&amt2000)
{
recvd=true;
notify();
}
}
}
class d implements Runnable
{
Bank b;
d(Bank b)
{
this.b=b;
new Thread(this,"deposit").start();
}
public void run()
{
while(true)
{
b.deposit();
}
}
}
class w implements Runnable
{
Bank b;
w(Bank b)
{
this.b=b;
new Thread(this,"withdraw").start();
}
public void run()
{
while(true)
{
b.withdraw();
}
}
}
public class MyBank
{
public static void main(String args[])
{
Bank b=new Bank();
new d(b);
new w(b);
}
}
Output
Balance less than 2000
Press Ctrl+b to terminate
Type amount to be deposited
500
Type an amount between 1 and 200
Balance less than 2000
Press Ctrl+b to terminate
Type amount to be deposited
200
Type an amount between 1 and 200
Balance less than 2000
Press Ctrl+b to terminate
Type amount to be deposited
150
Your current balance (d) is 750
Balance less than 2000
Press Ctrl+b to terminate
Type amount to be deposited
150
Your current balance (d) is 900
Balance less than 2000
Press Ctrl+b to terminate
Type amount to be deposited
150
Your current balance (d) is 1050
Balance less than 2000
Press Ctrl+b to terminate
Type amount to be deposited
150
Your current balance (d) is 1200
Balance less than 2000
Press Ctrl+b to terminate
Type amount to be deposited
150
Your current balance (d) is 1350
Balance less than 2000
Press Ctrl+b to terminate
Type amount to be deposited
150
Your current balance (d) is 1500
Balance less than 2000
Press Ctrl+b to terminate
Type amount to be deposited
150
Your current balance (d) is 1650
Balance less than 2000
Press Ctrl+b to terminate
Type amount to be deposited
150
Your current balance (d) is 1800
Balance less than 2000
Press Ctrl+b to terminate
Type amount to be deposited
150
Your current balance (d) is 1950
Balance less than 2000
Press Ctrl+b to terminate
Type amount to be deposited
150
Your current balance (d) is 2100
Type withdrawal amount
200
Type an amount between 1 and 150
Type withdrawal amount
100
Your current balance (w) is 2000
Type withdrawal amount
100
Your current balance (w) is 1900
Type withdrawal amount
150
Type an amount between 1 and 150
Type withdrawal amount
125
Your current balance (w)is 1775
Type withdrawal amount
Multithreading and Synchronization in Java is a post from ShoutToWorld - Let's Learn Let's Shout - Helping bloggers and developers.