Home‎ > ‎Applied Core Java‎ > ‎Threads‎ > ‎

Racing Condition

Two threads are simultaneously modifying a single object, that means both threads “race” to store their value, called racing condition.  The outcome of a program is affected by the order in which the program's threads are allocated CPU time

Problem
: Write a program that creates two threads. Both deposit 5000 Rs in the same account and go into racing condition.  Account.deposite() method is made synchronized so that only one thread can have access on it at a time, otherwise you will get incorrect balance.  You can experience incorrect balance if you remove synchronized keyword.

Program :

Step 1 - Coding

Create a text file c:\sunilos\Account.java  and copy below contents.

/*
 * A program implements Racing Condition
 */
   

public class Account {

    private int balance = 0;

    //public synchronized void deposit(String message, int amount) {
   
    /*
    Comment below statement and uncomment above statements to make deposit method synchronized
    
and get correct balance amount.
    */
   
    public void deposit(String message, int amount) {
        int bal;
        bal = getBalance() + amount;
        setBalance(bal);
        System.out.println(message + " Now Balance is " + bal);
    }

    public int getBalance() {
        try {
            Thread.sleep(200); // Simulate Database Operation
        } catch (InterruptedException e) {
        }
        return balance;
    }

    public void setBalance(int balance) {
        try {
            Thread.sleep(200);// Simulate Database Operation
        } catch (InterruptedException e) {
        }
        this.balance = balance;
    }

}

Step 2 - Creating Test Program


Create a text file c:\sunilos\RacingCondThread.java  and copy below contents.


/*
 * A program implements Racing Condition
 */

public class RacingCondThread extends Thread {
   
    public static Account data = new Account();
   
    private String name = null;

    public RacingCondThread(String name) {
       
        this.name = name;
    }
   
    public void run() {
       
        for(int i=0;i<5;i++){
            data.deposit(name, 1000);   
        }
       
    }
   
    public static void main(String[] args) {
        RacingCondThread user1 = new RacingCondThread("Dhoni");
        RacingCondThread user2 = new RacingCondThread("Yuvraj");
       
        user1.start();
        user2.start();

    }

}

Step 3 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy Account.java and  RacingCondThread.java into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile Account.java and RacingCondThread.java with help of
    •  javac Account.java command.
    •  javac RacingCondThread.java command. Compiled class file will be created in  the same folder.
  5. Congratulations!! your Test Java program is ready to serve.
  6. NOTE : You need to recompile Account class twice, one without synchronized deposit method and second with  synchronized deposit method.  And see differences

Step 4 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your  java program from command prompt with help of 
    • java RacingCondThread command

Output

When method is not synchronized 
    public void deposit(String message, int amount) {

Dhoni Now Balance is 1000
Yuvraj Now Balance is 1000
Dhoni Now Balance is 2000
Yuvraj Now Balance is 2000
Dhoni Now Balance is 3000
Yuvraj Now Balance is 3000
Yuvraj Now Balance is 4000
Dhoni Now Balance is 4000
Yuvraj Now Balance is 5000
Dhoni Now Balance is 5000

When method is synchronized
    public synchronized void deposit(String message, int amount) {

Dhoni Now Balance is 1000
Dhoni Now Balance is 2000
Dhoni Now Balance is 3000
Dhoni Now Balance is 4000
Dhoni Now Balance is 5000
Yuvraj Now Balance is 6000
Yuvraj Now Balance is 7000
Yuvraj Now Balance is 8000
Yuvraj Now Balance is 9000
Yuvraj Now Balance is 10000

FAQ

Q: How may types of synchronization are there?
A: Two type of synchronization

1. Method
public synchronized void deposit(String message, int amount) {….

2.Block
public void deposit(String message, int amount) {
synchronized (this){
int bal = getBalance() + amount;
setBalance(bal);
}
System.out.println(message + " Now Balance is " + bal);
}
ċ
Account.java
(1k)
Mahima Hardia,
Aug 19, 2011, 11:41 PM
ċ
RacingCondThread.java
(1k)
Mahima Hardia,
Aug 19, 2011, 11:42 PM
Comments