Race condition occurs in multithreaded environment where multiple threads execute the unsynchronized piece of code at the same time. These conditions majorly occur because of a programming fault in a parallel programming space. A race condition problem is mostly caused by common data\object access. When two or more threads try to operate on the same object member at the same time without proper synchronization, race condition may arise. It is also possible to face race condition when there is a sequence of atmoic operations in an unsynchronized block of code.

For example:

Let us say we have a class named as ‘Employee’. It has got one static member as ‘base_sal’ to denote the base salary of any employee of the company.

import java.io.Serializable;

@ClassMetadata(author="Username", purpose="class denoting an employee of an organization")
public class Employee implements Serializable {
	
	public static int base_sal = 0; 
	
	public static int getBase_sal() {
		return base_sal;
	}
	public static void setBase_sal(int base_sal) {
		Employee.base_sal = base_sal;
	}
	public int id;
	public String name;
	public String type;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}

}

Here, if one thread tries to change the base_sal value like this:

		if(Employee.base_sal == 0) {
			emp.setBase_sal(1000);
}

And another thread has some computation based on the base_sal value like this :

		if(Employee.base_sal == 0) {
              //some operations
		}	

Assume that the above blocks of code are not synchronized. When first thread enters into the if block and just before it sets the base_sal value, if the second thread evaluates the if condition, which in this case should evaluate to be ‘true’ the operations inside the if block will be performed, which is not the expected behaviour.

One of the solutions to avoid this race condition would be to make the setBase_sal() method synchronized like this :

	public static synchronized void setBase_sal(int base_sal) {
		Employee.base_sal = base_sal;
	}

Consider another example :

	public void method() {
		if(counter < 5) {
			counter = counter + 5;
			....
			....
			throw new Exception();
		}
	}

As this method or method body is not synchronized, if one thread enters inside the if block and increments its value by 5 (which say becomes more than 5) and then instead of successfully executing the whole block results into an exception. If the application demands that this block of code should be atomic meaning if no exception then only the counter should be incremented, then as it is not properly synchronized the race condition will occur. When another thread tries to check the if condition, it will evaluate to ‘false’ (which is wrong in the scenario just explained) and program behaviour is unexpected.

Solution to above race condition would be to make the whole method synchronized.

What is race condition? How will you avoid it? admin Core Java
Race condition occurs in multithreaded environment where multiple threads execute the unsynchronized piece of code at the same time. These conditions majorly occur because of a programming fault in a parallel programming space. A race condition problem is mostly caused by common data\object access. When two or more threads...
Race condition occurs in multithreaded environment where multiple threads execute the unsynchronized piece of code at the same time. These conditions majorly occur because of a programming fault in a parallel programming space. A race condition problem is mostly caused by common data\object access. When two or more threads try to operate on the same object member at the same time without proper synchronization, race condition may arise. It is also possible to face race condition when there is a sequence of atmoic operations in an unsynchronized block of code. <strong>For example:</strong> Let us say we have a class named as 'Employee'. It has got one static member as 'base_sal' to denote the base salary of any employee of the company. 1 <strong>Here, if one thread tries to change the base_sal value like this:</strong> 1 And another thread has some computation based on the base_sal value like this : 1 Assume that the above blocks of code are not synchronized. When first thread enters into the if block and just before it sets the base_sal value, if the second thread evaluates the if condition, which in this case should evaluate to be 'true' the operations inside the if block will be performed, which is not the expected behaviour. One of the solutions to avoid this race condition would be to make the setBase_sal() method synchronized like this : 1 Consider another example : 1 As this method or method body is not synchronized, if one thread enters inside the if block and increments its value by 5 (which say becomes more than 5) and then instead of successfully executing the whole block results into an exception. If the application demands that this block of code should be atomic meaning if no exception then only the counter should be incremented, then as it is not properly synchronized the race condition will occur. When another thread tries to check the if condition, it will evaluate to 'false' (which is wrong in the scenario just explained) and program behaviour is unexpected. Solution to above race condition would be to make the whole method synchronized.