Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Test1
- {
- public static void main(String args[])
- {
- Thread1 a=new Thread1(300);
- Thread2 b=new Thread2(200);
- Thread one=new Thread(a);
- Thread two=new Thread(b);
- try
- {
- one.start();
- two.start();
- one.join();
- two.join();
- }
- catch(InterruptedException e)
- {
- }
- //the parameters in the start method contains the amount to withdraw
- System.out.println(" The current balance is now = "+ Account.curr_bal);
- }
- }
- class Thread1 implements Runnable
- {
- int withdrawal;
- Thread1(int amt)
- {
- withdrawal=amt;
- }
- public void run()
- {
- Account obj1=new Account();
- obj1.withdraw(withdrawal);
- System.out.println(" Withdrawal complete successfully done by Thread1");
- }
- }
- class Thread2 implements Runnable
- {
- int withdrawal;
- Thread2(int amt)
- {
- withdrawal=amt;
- }
- public void run()
- {
- Account obj1=new Account();
- obj1.withdraw(withdrawal);
- System.out.println(" Withdrawal complete successfully done by Thread2");
- }
- }
- class Account
- {
- static int curr_bal=400;
- void withdraw(int amt)
- {
- if(curr_bal>amt)
- {
- try
- {
- Thread.sleep(1000);
- }
- catch(InterruptedException e)
- {
- }
- curr_bal=curr_bal-amt;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement