Synchronization

Java Synchronization: In-Depth Guide

1. What is Synchronization?

Definition

  • Mechanism to control multiple threads accessing shared resources

  • Prevents thread interference and memory consistency errors

  • Ensures thread safety and data consistency

Why Synchronization?

  1. Race Conditions: When multiple threads access shared data

  2. Data Inconsistency: Without sync, data can become corrupted

  3. Memory Visibility: Changes made by one thread may not be visible to others

2. Types of Synchronization

A. Method Synchronization

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

B. Block Synchronization

C. Static Synchronization

3. Key Synchronization Concepts

A. Monitor/Lock

  • Every object in Java has a monitor

  • Only one thread can own monitor at a time

  • synchronized keyword uses this intrinsic lock

B. Mutual Exclusion

C. Memory Visibility

4. Advanced Synchronization Techniques

A. ReentrantLock

B. ReadWriteLock

5. Common Interview Questions & Answers

Q1: What's the difference between synchronized method and synchronized block?

Answer:

  • Synchronized method locks entire method

  • Synchronized block locks specific section

  • Block synchronization is more efficient

  • Block allows finer control over lock duration

Q2: What is the 'volatile' keyword?

Answer:

  • Ensures variable is read from main memory

  • Prevents thread caching of variable

  • Provides visibility guarantee

  • Doesn't provide atomicity

  • Use cases: flags, status indicators

Q3: Explain object-level vs class-level locking

Answer:

  • Object-level: synchronized non-static methods/blocks

  • Class-level: synchronized static methods/blocks

  • Different locks, can run concurrently

  • Class lock is on Class object itself

Q4: What is thread starvation?

Answer:

  • Thread unable to gain regular access to shared resources

  • Causes: Priority issues, long-holding locks

  • Solution: Fair locks, proper timeout mechanisms

Q5: How to prevent deadlock?

Answer:

  1. Lock ordering

  2. Lock timeouts

  3. Try-lock mechanism

  4. Avoid nested locks

  5. Use Lock interface instead of synchronized

6. Common Synchronization Problems

A. Deadlock

B. Producer-Consumer Problem

7. Best Practices

Synchronization Guidelines

  1. Keep synchronized blocks small

  2. Don't synchronize immutable data

  3. Avoid synchronizing on String literals

  4. Use private final lock objects

  5. Consider using concurrent collections

  6. Prefer ReentrantLock for complex scenarios

  7. Document thread safety

Performance Considerations

  1. Minimize lock contention

  2. Use lock stripping when possible

  3. Consider using atomic classes

  4. Balance synchronization granularity

8. Modern Alternatives

A. Atomic Classes

B. Concurrent Collections

  • ConcurrentHashMap

  • CopyOnWriteArrayList

  • BlockingQueue

  • ConcurrentLinkedQueue

C. CompletableFuture (Java 8+)

Last updated