Difference in output from use of synchronized keyword and join()

Posted by user2964080 on Stack Overflow See other posts from Stack Overflow or by user2964080
Published on 2013-11-07T09:45:57Z Indexed on 2013/11/07 9:53 UTC
Read the original article Hit count: 373

Filed under:
|
|
|

I have 2 classes,

public class Account {
private int balance = 50;

public int getBalance() {
    return balance;
}
public void withdraw(int amt){
    this.balance -= amt;
} }

and

public class DangerousAccount implements Runnable{
private Account acct = new Account();   

public static void main(String[] args) throws InterruptedException{
    DangerousAccount target = new DangerousAccount();
    Thread t1 = new Thread(target);
    Thread t2 = new Thread(target);

    t1.setName("Ravi");
    t2.setName("Prakash");
    t1.start();
/* #1 t1.join(); */
    t2.start();
}

public void run(){
    for(int i=0; i<5; i++){
        makeWithdrawl(10);
        if(acct.getBalance() < 0)
            System.out.println("Account Overdrawn");
    }
}
  public void makeWithdrawl(int amt){
        if(acct.getBalance() >= amt){
            System.out.println(Thread.currentThread().getName() + " is going to withdraw");
            try{
                Thread.sleep(500);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            acct.withdraw(amt);
      System.out.println(Thread.currentThread().getName() + " has finished the withdrawl");
    }else{
        System.out.println("Not Enough Money For " + Thread.currentThread().getName() + " to withdraw");
}
  }
}

I tried adding synchronized keyword in makeWithdrawl method

public synchronized void makeWithdrawl(int amt){

and I keep getting this output as many times I try

Ravi is going to withdraw
Ravi has finished the withdrawl
Ravi is going to withdraw
Ravi has finished the withdrawl
Ravi is going to withdraw
Ravi has finished the withdrawl
Ravi is going to withdraw
Ravi has finished the withdrawl
Ravi is going to withdraw
Ravi has finished the withdrawl
Not Enough Money For Prakash to withdraw
Not Enough Money For Prakash to withdraw
Not Enough Money For Prakash to withdraw
Not Enough Money For Prakash to withdraw
Not Enough Money For Prakash to withdraw

This shows that only Thread t1 is working...

If I un-comment the the line saying

t1.join();

I get the same output.

So how does synchronized differ from join() ?

If I don't use synchronize keyword or join() I get various outputs like

Ravi is going to withdraw
Prakash is going to withdraw
Prakash has finished the withdrawl
Ravi has finished the withdrawl
Prakash is going to withdraw
Ravi is going to withdraw
Prakash has finished the withdrawl
Ravi has finished the withdrawl
Prakash is going to withdraw
Ravi is going to withdraw
Prakash has finished the withdrawl
Ravi has finished the withdrawl
Account Overdrawn
Account Overdrawn
Not Enough Money For Ravi to withdraw
Account Overdrawn
Not Enough Money For Prakash to withdraw
Account Overdrawn
Not Enough Money For Ravi to withdraw
Account Overdrawn
Not Enough Money For Prakash to withdraw
Account Overdrawn

So how does the output from synchronized differ from join() ?

© Stack Overflow or respective owner

Related posts about java

Related posts about multithreading