VOOZH about

URL: https://www.geeksforgeeks.org/java/synchronization-in-java/

⇱ Synchronization in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Synchronization in Java

Last Updated : 24 Apr, 2026

Synchronization is used to control the execution of multiple processes or threads so that shared resources are accessed in a proper and orderly manner. It helps avoid conflicts and ensures correct results when many tasks run at the same time.

  • It controls the access of shared resources.
  • It avoids data inconsistency.
  • It ensures proper execution of processes.

Ways to Achieve Synchronization

There are three main ways to achieve synchronization.

👁 synchronizatin

1. Synchronized Methods

Synchronized methods are used to lock an entire method so that only one thread can execute it at a time for a particular object. This ensures safe access to shared data but may reduce performance due to full method locking.

  • Locks the whole method, not just a part of it.
  • Uses the object-level lock (instance lock).

Output
Counter: 2000

Explanation: Both threads increment the same counter concurrently. Since the inc() and get() methods are synchronized, only one thread can access them at a time, ensuring the correct final count.

2. Synchronized Blocks

Synchronized blocks allow locking only a specific section of code instead of the entire method. This makes the program more efficient by reducing the scope of synchronization.

  • Locks only the critical section of code, not the entire method.
  • Provides better performance due to fine-grained control.

Output
Counter: 2000

Explanation: The synchronized block ensures mutual exclusion only for the increment statement, reducing the locking overhead.

3. Static Synchronization

Static synchronization is used when static data or methods need to be protected in a multithreaded environment. It ensures that only one thread can access the class-level resource at a time.

  • Locks at the class level instead of the object level.
  • Shared across all instances of the class.

Output
1
2
3
10
20
30

Explanation: Both threads t1 and t2 call the static synchronized method printTable(). The lock is applied to the Table.class object, ensuring that only one thread can access the method at a time, even if no object instance is shared.

Types of Synchronization

There are two type of synchronizations in Java which are listed below:

1. Process Synchronization

Process synchronization is a fundamental concept in operating systems that ensures multiple processes or threads can execute safely while sharing common resources. Process Synchronization is a technique used to coordinate the execution of multiple processes. It ensures that the shared resources are safe and in order.

  • Prevents race conditions by controlling access to shared resources.
  • Ensures data consistency and integrity in concurrent execution.
  • Uses mechanisms like semaphores, mutex locks, and monitors for coordination.

Output
Deposited: 200, Balance: 1200
Withdrawn: 100, Balance: 1100
Deposited: 200, Balance: 1300
Deposited: 200, Balance: 1500
Withdrawn: 100, Balance: 1400
Withdrawn: 100, Balance: 1300
Final Balance: 1300

Explanation: Two threads perform deposit and withdrawal operations simultaneously. The synchronized methods prevent race conditions, ensuring consistent balance updates.

2. Thread Synchronization in Java

Thread Synchronization is used to coordinate and ordering of the execution of the threads in a multi-threaded program. There are two types of thread synchronization are mentioned below:

Example: Ticket Booking System


Output
Booked 2 tickets, Remaining tickets: 8
Booked 3 tickets, Remaining tickets: 5
Booked 3 tickets, Remaining tickets: 2
Booked 2 tickets, Remaining tickets: 0
Final Available Tickets: 0

Explanation: The synchronized bookTicket() method ensures that only one thread books tickets at a time, preventing overbooking and ensuring correct availability.

Volatile Keyword

The volatile keyword in Java ensures that all threads have a consistent view of a variable's value. It prevents caching of the variable's value by threads, ensuring that updates to the variable are immediately visible to other threads.

Working of Volatile Modifier:

  • It applies only to variables.
  • volatile guarantees visibility i.e. any write to a volatile variable is immediately visible to other threads.
  • It does not guarantee atomicity, meaning operations like count++ (read-modify-write operations) can still result in inconsistent values

Output
Running...
Running...
Running...
Stopped.

Explanation: The volatile variable running ensures that updates made by one thread (in stop()) are visible to the thread running the loop in start().

Volatile vs Synchronized

volatilesynchronized
Ensures visibility of changes across threadsEnsures visibility along with mutual exclusion
Does not use any lockingUses intrinsic locking mechanism
Not thread-safe for complex operationsFully thread-safe
Does not guarantee atomicityGuarantees atomic operations
Lightweight and fasterHeavyweight and slower due to locking
Used for simple flags or variablesUsed for critical sections like counters or transactions
Comment
Article Tags: