← Back to list

Advanced Java Thread Safety Using Object Synchronization

In modern backend systems, multiple threads often access shared resources simultaneously. Without proper synchronization, applications…

Srinivas Palli · 2026-05-26 01:12 · 1 claps · 2.4 min read
#java #synchronization #object-locking #synchronizing-objects #threads
Open on Medium ↗
Wiki topics: SAF · Safety & Alignment 🌐 · Web Development

Advanced Java Thread Safety Using Object Synchronization

In modern backend systems, multiple threads often access shared resources simultaneously. Without proper synchronization, applications become unpredictable, unsafe, and difficult to debug.

Java provides synchronization mechanisms that help developers ensure thread safety and maintain data consistency.

In this article, we’ll deeply explore:

  • Object-level synchronization
  • Intrinsic locks
  • Monitor locks
  • Race conditions
  • Synchronized methods vs blocks
  • JVM internals
  • Performance considerations
  • Best practices for scalable applications

Understanding the Problem: Race Conditions

A race condition occurs when multiple threads modify shared data simultaneously without coordination.

Consider a banking application:

class BankAccount {
    int balance = 1000;

    void withdraw(int amount) {
        balance = balance - amount;
    }
}

If two threads withdraw money at the same time, the final balance may become inconsistent.

This happens because:

  1. Thread A reads balance
  2. Thread B reads same balance
  3. Both modify independently
  4. One update overwrites another

Result: Corrupted data Unpredictable application behavior

What is Synchronization in Java?

Synchronization is a mechanism that restricts multiple threads from accessing shared resources simultaneously.

Java uses an intrinsic locking mechanism called:

  • Monitor Lock
  • Intrinsic Lock
  • Object Lock

Every Java object has an associated monitor lock.

When a thread enters a synchronized block:

  • It acquires the object’s monitor
  • Other threads must wait
  • Lock releases automatically after execution

Object-Level Synchronization

Object-level synchronization locks a specific object instance.

class Counter {
    private int count = 0;   
    public synchronized void increment() {
        count++;
    }
}

Here:

  • increment() locks the current object (this)
  • Only one thread per object can execute synchronized methods simultaneously

Different objects can still execute concurrently.

How JVM Handles Synchronization

Behind the scenes, JVM uses:

  • monitorenter
  • monitorexit

bytecode instructions.

When entering synchronized code:

  1. Thread checks monitor availability
  2. Acquires monitor
  3. Executes critical section
  4. Releases monitor automatically

This ensures:

  • Mutual exclusion
  • Visibility guarantees
  • Memory consistency

Synchronized Block vs Synchronized Method

Synchronized Method

public synchronized void update() {
    // critical section
}

Locks entire object.

Synchronized Block

public void update() {
    synchronized(this) {
        // critical section
    }
}

Locks only selected code section.

Advantages:

  • Better performance
  • Smaller lock scope
  • Reduced thread contention

Advanced Example: Thread-Safe Bank Account

class BankAccount {
    private int balance = 1000;
   public synchronized void deposit(int amount) {
        balance += amount;
    }
    public synchronized void withdraw(int amount) {
        balance -= amount;
    }
   public synchronized int getBalance() {
        return balance;
    }
}

This guarantees:

  • Consistent updates
  • No race conditions
  • Thread-safe operations

Object Locking vs Class Locking

Object Lock

public synchronized void method() {}

Locks current object instance.

Class Lock

public static synchronized void method() {}

Locks the Class object.

Used when:

  • Static shared resources exist
  • Global synchronization required

Performance Considerations

Synchronization improves safety but introduces overhead.

Potential issues:

  • Thread contention
  • Blocking
  • Context switching
  • Deadlocks

Modern JVM optimizations include:

  • Biased locking
  • Lightweight locking
  • Lock coarsening
  • Lock elimination

Best Practices

Keep Critical Sections Small

Bad:

synchronized(this) {
    // huge logic
}

Good:

synchronized(this) {
    count++;
}

Avoid Synchronizing Entire Methods Unnecessarily

Prefer synchronized blocks for fine-grained locking.

Use Private Lock Objects

private final Object lock = new Object();
synchronized(lock) {
    // safer locking
}

This prevents external interference.

Common Mistakes

1. Synchronizing on String Literals

synchronized("LOCK") {}

Dangerous because strings are pooled.

2. Nested Locks

Can cause deadlocks.

3. Excessive Synchronization

Reduces scalability and throughput.

Modern Alternatives

Java also provides advanced concurrency utilities:

  • ReentrantLock
  • ReadWriteLock
  • StampedLock
  • AtomicInteger
  • ConcurrentHashMap

These often provide:

  • Better scalability
  • More control
  • Higher performance

Final Thoughts

Synchronization is fundamental for building reliable multithreaded Java applications.

Understanding:

  • Object monitors
  • Intrinsic locks
  • JVM behavior
  • Lock granularity

helps developers write highly scalable and thread-safe systems.

Mastering synchronization is one of the biggest steps toward becoming an advanced Java engineer.

Best Medium Tags

Use these tags on Medium:

  • Java
  • Multithreading
  • Concurrency

This happens because:

  1. Thread A reads balance
  2. Thread B reads same balance
  3. Both modify independently
  4. One update overwrites another

Result:

  • Corrupted data
  • Unpredictable application behavior

메타데이터
post_id
7f3aae73c3bb
slug
advanced-java-thread-safety-using-object-synchronization-7f3aae73c3bb
url
https://medium.com/@srinivaspalli/advanced-java-thread-safety-using-object-synchronization-7f3aae73c3bb
canonical_url
https://medium.com/@srinivaspalli/advanced-java-thread-safety-using-object-synchronization-7f3aae73c3bb
author_url
https://medium.com/@srinivaspalli
status
ok
fetched_at
2026-06-09 14:34:10