|
| 1 | +package practice; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | +class HusbendThread159 implements Runnable{ |
| 5 | + Thread husbend; |
| 6 | + SBI s; |
| 7 | + Scanner sc=new Scanner(System.in); |
| 8 | + HusbendThread159(SBI s) |
| 9 | + { |
| 10 | + this.s=s; |
| 11 | + husbend=new Thread(this,"Hitesh"); |
| 12 | + husbend.start(); |
| 13 | + } |
| 14 | + public void run() |
| 15 | + { try { |
| 16 | + synchronized (s) { |
| 17 | + System.out.println("Balance is before deposite :"+s.balance); |
| 18 | + |
| 19 | + System.out.println("Enter the deposite value"); |
| 20 | + int hdeposite=sc.nextInt(); |
| 21 | + |
| 22 | + s.balance=s.deposite(hdeposite); |
| 23 | + |
| 24 | + System.out.println("After depsosite amount is" +s.balance); |
| 25 | + } |
| 26 | + }catch(Exception i) |
| 27 | + { |
| 28 | + System.out.println(i); |
| 29 | + } |
| 30 | + |
| 31 | + } |
| 32 | +} |
| 33 | +class WifeThread159 implements Runnable{ |
| 34 | + Thread wife; |
| 35 | + SBI s; |
| 36 | + Scanner sc=new Scanner(System.in); |
| 37 | + WifeThread159(SBI s) |
| 38 | + { |
| 39 | + this.s=s; |
| 40 | + wife=new Thread(this,"Hitesh"); |
| 41 | + wife.start(); |
| 42 | + } |
| 43 | + public void run() |
| 44 | + { try { |
| 45 | + synchronized (s) { |
| 46 | + System.out.println("Balance is before withdraw :"+s.balance); |
| 47 | + |
| 48 | + System.out.println("Enter the withdraw value"); |
| 49 | + int Wwithdraw=sc.nextInt(); |
| 50 | + |
| 51 | + s.balance=s.withdraw(Wwithdraw); |
| 52 | + |
| 53 | + System.out.println("After withdraw amount is" +s.balance); |
| 54 | + } |
| 55 | + }catch(Exception i) |
| 56 | + { |
| 57 | + System.out.println(i); |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +class SBI{ |
| 65 | + volatile int balance=50000; |
| 66 | + private int deposite; |
| 67 | + private int withdraw; |
| 68 | + Scanner sc=new Scanner(System.in); |
| 69 | + |
| 70 | + public int deposite(int hdeposite) |
| 71 | + { |
| 72 | + try { |
| 73 | + balance=balance+hdeposite; |
| 74 | + Thread.sleep(500); |
| 75 | + |
| 76 | + }catch(InterruptedException k) |
| 77 | + { |
| 78 | + System.out.println(k); |
| 79 | + } |
| 80 | + return balance; |
| 81 | + } |
| 82 | + public int withdraw(int Wwithdraw) |
| 83 | + { |
| 84 | + try { |
| 85 | + balance=balance-Wwithdraw; |
| 86 | + Thread.sleep(500); |
| 87 | + |
| 88 | + }catch(InterruptedException k) |
| 89 | + { |
| 90 | + System.out.println(k); |
| 91 | + } |
| 92 | + return balance; |
| 93 | + } |
| 94 | +} |
| 95 | +public class ThreadDemo18 { |
| 96 | + |
| 97 | +} |
0 commit comments