Race Condition in Java
Java is a multi-threaded programming language and there is a higher risk to occur race conditions. Because the same resource may be accessed by multiple threads at the same time and may change the data. We can say that race condition is a concurrency bug. It is closely related to deadlock in Java. In this section, we will implement the race condition in Java.
What is race condition?
A condition in which the critical section (a part of the program where shared memory is accessed) is concurrently executed by two or more threads. It leads to incorrect behavior of a program.
In layman terms, a race condition can be defined as, a condition in which two or more threads compete together to get certain shared resources.
For example, if thread A is reading data from the linked list and another thread B is trying to delete the same data. This process leads to a race condition that may result in run time error.
There are two types of race conditions:
The read-modify-write patterns signify that more than one thread first read the variable, then alter the given value and write it back to that variable. Let's have a look at the following code snippet.
Why it occurs?
It occurs when two or more threads operate on the same object without proper synchronization and their operation incorporates each other.
Example of Race condition
Suppose, there are two processes A and B that are executing on different processors. Both processes are trying to call the function bankAccount() concurrently. The value of the shared variable that we are going to pass in the function is 1000.
Consider, A call the function bankAccount() and passing a value 200 as a parameter. In the same way, process B is also calling the function bankAccount() and passing a value of 100 as a parameter.
The result look like as follows:
Value for Thread After increment Thread-1 2 Value for Thread at last Thread-1 2 Value for Thread After increment Thread-3 3 Value for Thread at last Thread-3 1 Value for Thread After increment Thread-2 2 Value for Thread at last Thread-2 0
In the above output, we can observe that variable c is giving wrong values.
How to avoid it?
There are the following two solutions to avoid race conditions.
In order to prevent the race conditions, one should ensure that only one process can access the shared data at a time. It is the main reason why we need to synchronize the processes.
Another solution to avoid race condition is mutual exclusion. In mutual exclusion, if a thread is using a shared variable or thread, another thread will exclude itself from doing the same thing.
Let's see a Java program for the same.
Value for Thread After increment Thread-1 1 Value for Thread at last Thread-1 0 Value for Thread After increment Thread-3 1 Value for Thread at last Thread-3 0 Value for Thread After increment Thread-2 1 Value for Thread at last Thread-2 0
It can be seen from the output how threads are accessing the shared resource one at a time now. Synchronizing the access within the run() method made it happen.
Next TopicStatic Array in Java