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()
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&&wamt<150) go to 8 else to 10.
Step 8: bal=bal-wamt.
Step 9: Print bal go to 11.
Step 10: Print “Type an amount between 1 and 150”.
Step 11: if(bal<500) go to 12 else to 15.
Step 12: Print “Balance less than 500”.
Step 13: recvd=false.
Step 14: notify().
Step 15: Exit.
Function synchronized void deposit()
Step 1: Start.
Step 2: if(!recvd) go to 3 else to 4..
Step 3: wait().
Step 4: Print “Balance less than 2000,press ctrl+b to terminate”.
Step 5: Read amt.
Step 6: if(amt>1&&amt<200) go to 7 else to 9.
Step 7: bal=bal+amt.
Step 8: Print bal go to 10.
Step 9: Print “Type an amount between 1 and 200”.
Step 10: if(bal>2000) 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&&wamt<150) { bal=bal-wamt; System.out.println("Your current balance(w)is "+bal); } else { System.out.println("Type an amount between 1 and 150"); } } } catch(Exception e) { System.out.println("Error : " + e ); } if(bal<500) { System.out.println("Balance less than 500"); recvd=false; notify(); } } synchronized void deposit() { if(recvd) try { wait(); } catch(Exception e) { System.out.println("Error : " + e ); } try { System.out.println("Balance less than 2000"); System.out.println("Press Ctrl+b to terminate"); InputStreamReader ins=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(ins); System.out.println("Type amount to be deposited"); amt=Integer.parseInt(br.readLine()); if(amt>1&&amt<200) { bal=bal+amt; System.out.println("Your current balance(d)is "+bal); } else { System.out.println("Type an amount between 1 and 200"); } } catch(Exception e) { System.out.println("Error : " + e ); } if(bal>2000) { 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