Java Race Condition with Example

Sometimes, people detect some error in the program’s compile-time or run time, which mainly occurs due to race condition while executing programs. Are you excited to know more about race condition?

This article will give a piece of detailed information on Race conditions in Java. Before proceeding to know about a race condition, let’s focus on threading. This will help you to understand the basics of race conditions.

What is a thread, and why is it used?

In java circumstances, the thread is the path that is followed when the program execution takes place. All the programs in Java at least have one thread, known as the main thread. This thread is created by JVM (java virtual machine). The main() method in Java is invoked with the main thread when the program starts to execute.

In Java, by implementing an interface and spaning a class, you can create a thread. Java thread is controlled and created by Java.Lang.Thread class.

An application that has single-threaded can perform only one task at a time. To perform multiple tasks parallelly, multi-threads are introduced. Each thread performs a particular job.

Multi-threading is extensively used for faster processing in background programs. Apart from this, the other uses are:

  1. It reduces the ideal time and response time of the CPU
  2. Takes the full advantages of modern processors that have multiple cores and deal with multiple users simultaneously.

So, now you might be clear about the thread concept, so let’s move to Java’s race condition.

Race Condition in Java

Race condition in Java is the type of simultaneous bug or an obstacle that arises in the execution of a program due to parallel implementation of the programs using multiple threads at the same time. As we know, Java is a multithreaded programming language; therefore, the possibility of race conditions in Java is high.

What Causes Race Conditions and Types of Race Conditions

The race condition is just a hazardous thing exhibited due to multi-threading, and somewhat this condition is similar to the deadlock condition in Java.

The leading cause of a Race condition is when two or more threads working on the same object or data without proper synchronization result in overlapping in operation on each other.

Two types of race conditions:

  1. Read-modify-write
  2. Check-then-act

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. To know the race condition in the read-modify-write pattern, take a closer look through the code.

When thread A and thread B work on adding the same object of the class, the code is written in function add() will not get executed in single instruction by Java virtual machine. Instead, it will run in a set of more minor instructions. Those minor instructions are

  1. Read this.number from memory into a register.
  2. Add value to register.
  3. Write register to memory.

Let’s see what happens when we do mixed execution of thread A and Thread B.

this.number is initialized to zero;

this.number=0;

The process of thread A and Thread B is

A: Reads this. number put it into a register with value 0

B: Reads this. number into a register with value 0

B: Adds value 4 to register

B: Writes register value with four gets it to return to the memory, this. number is equal to 4

A: Adds 5 to the register with zero.

A: Writes register value with five get it to return to the memory. this. number now equals 5

The two threads want to add 4 and 5 to the number class. However, the result should be nine after the completion of the two threads. The thread’s operations interleave with each other. Therefore the result seems to be different.

In the given sequence of the two threads, both the threads read the value zero from memory. After that, the threads add 4 and 5 to the zero and result back to memory. Instead of 9, the value that is left in this. number is different. This will raise race condition.

* In the check-then-act pattern, if multiple threads monitor the same condition, then according to the condition, one thread’s execution changes it, leading to a race condition. In others words, if two or more thread works on the same condition simultaneously, then out of all the thread one will thread go ahead and change the condition. This will lead to another thread working incorrectly due to a change in condition. And the race situation occurs.

To illustrate, look at the code:

If multiple threads call the example() of the class at the same time, they execute the “if condition” simultaneously. However, one thread will take the value, and the other thread will take the null value back.

How to Prevent Race Condition?

To obstruct the race condition from occurring, make sure the program is executed on atomic instruction. That means only one thread is running at the given time, and no other threads execute until the first thread completes its execution in the critical section.

By maintaining proper thread synchronization, you can avoid the race condition.

Conclusion

This race condition is an important topic to understand in Java because there will be the situation under which you want to avoid the hindering situation, so after reading this article, you get a brief explanation of race conditions in Java with all the details about types and prevention.

Leave a Comment

Your email address will not be published. Required fields are marked *