Table of contents
1.
Introduction
2.
What is Synchronization in Java?
3.
Why Use Synchronization?
4.
Types of Synchronization
4.1.
1. Process Synchronization
4.2.
2. Thread Synchronization
4.3.
3. Static Synchronization
5.
Concept of Synchronization
6.
What are Locks in Java?
7.
Java Synchronized Method
8.
Understanding the Problem without Java Synchronization Method
8.1.
Java
9.
Understanding the Problem with Java Synchronization Method
9.1.
Java
10.
Example of Synchronized Method by Using an Anonymous Class
10.1.
Java
11.
Frequently Asked Questions
11.1.
What is a synchronized class?
11.2.
When to use synchronized Java?
11.3.
What is Synchronization in Java?
11.4.
Why do we need to use synchronization in multithreaded programming?
11.5.
What are the benefits of synchronization in Java?
11.6.
When to use synchronized in Java?
12.
Conclusion
Last Updated: Sep 18, 2025
Easy

Synchronization in Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Synchronization in Java is a fundamental concept for managing concurrent threads and ensuring data consistency in multithreaded applications. When multiple threads access shared resources, synchronization prevents data corruption by coordinating access, avoiding race conditions, and ensuring that operations are performed atomically. In this blog, we’ll explore the importance of synchronization.

Synchronization in Java

What is Synchronization in Java?

Synchronization in Java is a mechanism that ensures controlled access to shared resources by multiple threads. When multiple threads try to access and modify the same object or variable simultaneously, it can lead to inconsistent or unpredictable results. Synchronization prevents such issues by allowing only one thread at a time to execute a block or method that is marked as synchronized.

It is commonly used in multithreaded applications to maintain data integrity and ensure thread safety.

Why Use Synchronization?

  • Prevent Data Inconsistencies: Synchronization ensures data consistency by controlling access to shared resources.
  • Avoid Race Conditions: It helps prevent threads from interfering with each other’s operations.
  • Ensure Thread Safety: Synchronization enables atomic, secure operations on shared data in multithreaded applications.

Types of Synchronization

1. Process Synchronization

Manages resources across different processes to avoid conflicts, ensuring only one process accesses a shared resource at a time.

2. Thread Synchronization

Controls multiple threads within the same application, using methods like synchronized blocks, synchronized methods, and locks to ensure threads don’t concurrently modify shared resources.

3. Static Synchronization

Involves synchronizing static methods or blocks to ensure that only one thread can access a static resource of a class at any given time.

Concept of Synchronization

Synchronization in Java refers to the ability to govern different threads' access to a shared resource. When we only want one thread to access a shared resource, Java Synchronization is a better solution. The basic purpose of synchronization is to avoid thread interference and solve the random-access problem. This blog is mainly concerned with Thread Synchronization Techniques. 

There are two types of thread synchronization in Java:

  1. Mutual Exclusion(When only one thread can access a shared space, resource, etc.)
  2. Cooperation(or Inter-thread communication)

 

Mutually exclusive helps keep threads from interfering with one another while sharing data. It can be achieved by using the following three ways:

  1. By Using the Synchronized Method
  2. By Using Synchronized Block
  3. By Using Static Synchronization

What are Locks in Java?

Synchronization is based on a lock or monitor, which is an internal entity. A lock is associated with every object. By convention, a thread that requires consistent access to an object's fields must first obtain the object's lock and then release it after it's finished. All other threads attempting to access the object’s field are blocked until the thread currently holding the object’s lock releases it.

The package java.util.concurrent.locks contain many lock implementations starting with Java 5.

Java Synchronized Method

If you declare any method as synchronized, it is known as a synchronized method. The synchronized method is used to lock an object for any shared resource. When a thread calls a synchronized method, the lock for that object is automatically acquired and released when the thread completes its task.

Understanding the Problem without Java Synchronization Method

Let us first try this multi-threaded program without synchronization, and try it on Java online compiler.

  • Java

Java

//Example of Java Synchronized Method
class ResourceClass {

int resource;

ResourceClass(int val) {
  this.resource = val;
}

void UpdateandPrintResource(int n, int threadNum) {
  System.out.println("Thread " + threadNum + " Starts....");
  this.resource += n;
  try {
    Thread.sleep(400);
  } catch (Exception e) {
    System.out.println(e);
  }
  System.out.println("Value of Resource: "+this.resource);
  System.out.println("Thread " + threadNum + " Ends....");
}
}

class T1 extends Thread {

ResourceClass t;

T1(ResourceClass t) {
  this.t = t;
}

public void run() {
  t.UpdateandPrintResource(5, 1);
}
}

class T2 extends Thread {

ResourceClass t;

T2(ResourceClass t) {
  this.t = t;
}

public void run() {
  t.UpdateandPrintResource(10, 2);
}
}

public class TestSynchronization {

public static void main(String args[]) {
  ResourceClass resoruceInst = new ResourceClass(10);
  T1 t1 = new T1(resoruceInst);
  T2 t2 = new T2(resoruceInst);
  t1.start();
  t2.start();
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

As you can see, there is no order in which the object is accessed and its function is called. This is the problem with no synchronization, there is no consistency in the order of accessing and updating any resource.

Understanding the Problem with Java Synchronization Method

Now let us see the same program with the synchronized method.

  • Java

Java

//Example of Java Synchronized Method
class ResourceClass {

int resource;

ResourceClass(int val) {
  this.resource = val;
}

synchronized void UpdateandPrintResource(int n, int threadNum) {
  System.out.println("Thread " + threadNum + " Starts....");
  this.resource += n;
  try {
    Thread.sleep(400);
  } catch (Exception e) {
    System.out.println(e);
  }
  System.out.println("Value of Resource: "+this.resource);
  System.out.println("Thread " + threadNum + " Ends....");
}
}

class T1 extends Thread {

ResourceClass t;

T1(ResourceClass t) {
  this.t = t;
}

public void run() {
  t.UpdateandPrintResource(5, 1);
}
}

class T2 extends Thread {

ResourceClass t;

T2(ResourceClass t) {
  this.t = t;
}

public void run() {
  t.UpdateandPrintResource(10, 2);
}
}

public class TestSynchronization {

public static void main(String args[]) {
  ResourceClass resoruceInst = new ResourceClass(10);
  T1 t1 = new T1(resoruceInst);
  T2 t2 = new T2(resoruceInst);
  t1.start();
  t2.start();
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

Example of Synchronized Method by Using an Anonymous Class

  • Java

Java

class Counter {
private int count = 0;

// Synchronized method to increment the count
public synchronized void increment() {
count++;
}

public int getCount() {
return count;
}
}

public class SynchronizedAnonymousClassExample {
public static void main(String[] args) {
Counter counter = new Counter();

// Creating multiple threads using an anonymous class
Thread thread1 = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
}
});

Thread thread2 = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
}
});

// Starting the threads
thread1.start();
thread2.start();

try {
// Wait for both threads to finish
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

// Output the final count
System.out.println("Final count: " + counter.getCount());
}
}
You can also try this code with Online Java Compiler
Run Code

Output

Final count: 2000

Frequently Asked Questions

What is a synchronized class?

A synchronized class in Java ensures thread-safe operations by allowing only one thread to access its methods at a time, preventing data inconsistency.

When to use synchronized Java?

Use synchronization in Java when multiple threads access shared resources to avoid race conditions, ensure thread safety, and maintain consistent application behavior.

What is Synchronization in Java?

Synchronization in java is the capability to control the access of multiple threads to any shared resource. In the Multithreading concept, multiple threads try to access the shared resources at a time to produce inconsistent results. Synchronization is necessary for reliable communication between threads.

Why do we need to use synchronization in multithreaded programming?

We need to synchronize the shared resources to ensure that at a time only one thread is able to access the shared resource. If an object is shared by multiple threads then there is a need for synchronization in order to avoid the Object's state getting corrupted.

What are the benefits of synchronization in Java?

Synchronization in Java ensures thread safety by preventing data corruption, avoiding race conditions, and maintaining data consistency across multiple threads. It enhances reliability in concurrent applications, allowing for coordinated access to shared resources and ensuring atomicity of operations.

When to use synchronized in Java?

Use synchronized when multiple threads access shared resources, such as variables or data structures, that require consistent states. It's essential in scenarios involving critical sections, where operations must be executed atomically to avoid data inconsistencies or unexpected behavior.

Conclusion

In this article, we have discussed Synchronization in Java. Synchronization is a crucial aspect of Java programming that enables developers to manage concurrent access to shared resources effectively. By implementing synchronization techniques, such as synchronized methods and blocks, Java applications can ensure thread safety and data integrity, preventing common issues like race conditions and data corruption.

Recommended Readings:

Live masterclass