Synchronization in Java: All You Need to Know

Synchronization in Java: All You Need to Know

In this article, I’m going to discuss race conditions, why they occur, and finally how to handle it using a synchronizedkeyword.

In this article, I’m going to discuss race conditions, why they occur, and finally how to handle it using a **synchronized**keyword.

Multi-threading is one of the essential skills for every java developer. I was quite interested before writing this article as I take this opportunity to share my thoughts and knowledge with you guys about Java synchronization.

Race Condition :

In a multi-threaded environment, when two or more threads try to update the mutable shared resource(data) at the same time, a race condition may occur because of context switching between the threads.

Race Condition example → Increment Operation:

1\. x = 100;
2\. for ( int i = 0; i < 10; i++ )
3\. {
4\.       x = x + 1; 
5\. }

In this example, if we run two threads *t1 *and *t2 *concurrently (in parallel) executing the above code, then the value of *x *may not be *120 *and the result would vary every time we run the code. This is happening because of thread context switching.

Let’s see what actually happens when the value of *x *is incremented.

Read the value of x
Add 1 to this value
Store this value to x

We can see that the increment is not an atomic step but it is happening in 3 steps.

These two threads can be at any step in this process at any time, and there can be context switching when a shared resource is involved. The state of x can be changed by another thread during the time between x is being read and when it is written back.

Let’s say a thread t1 *retrieves the value of *x *but not yet incremented. Another thread *t2 *can also retrieve the *same value of x (because thread t1has changed it yet) and then they would both be storing the same value (x+1) back in x.

synchronization programming synchronized java-programming race-condition

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java Core (2020) | Java for beginners | Brush up your Java Skills | Get Hired

In this video we will learn about all the major concepts that come under Java Core . The course is very carefully designed and is made with keeping simplicit...

Java Synchronized Example | Synchronization in Java Tutorial

Java Synchronized Example | Synchronization in Java Tutorial. It is a safeguard that the resource is being used by only one thread at a time.

Java If-else Statement | Conditional Statement in Java Example

Java if-else statement is used to test a condition. It checks for the boolean condition: true or false. There are various kinds of if statement in Java.

Introduction To Java In Hindi | Learn Java Programming | Java Basics

Introduction To Java In Hindi | Learn Java Programming | Java Basics. This article Session on Introduction to Java in Hindi will cover the 'what, why, and how' of Java. This will include features, data types, operators, conditional statements, and loops.