← Back to Questions
Java

Use of synchronized keyword annd ConcurrentHashMap.

Learn Use of synchronized keyword annd ConcurrentHashMap. with simple explanations, real-time examples, interview tips and practical use cases.

Use of synchronized Keyword and ConcurrentHashMap in Java

Introduction

In Java concurrency, synchronization is critical to prevent race conditions when multiple threads access shared resources. The synchronized keyword is the simplest way to achieve mutual exclusion, while ConcurrentHashMap is a high-performance, thread-safe collection designed to avoid explicit synchronization.

Use of synchronized Keyword

The synchronized keyword ensures that only one thread can execute a block of code or method at a time. It can be applied to methods or code blocks.


class Counter {
    private int count = 0;

    public synchronized void increment() {
        count++;
    }

    public synchronized int getCount() {
        return count;
    }
}
  

Explanation: The synchronized methods ensure that increments and reads are atomic, preventing race conditions.

Limitations of synchronized

  • Can cause contention and reduce performance when many threads compete.
  • Locks are coarse-grained — entire method or block is locked.
  • Can lead to deadlocks if not used carefully.

Use of ConcurrentHashMap

ConcurrentHashMap is a thread-safe alternative to HashMap. It allows concurrent read operations and segmented write operations, avoiding global locks.


import java.util.concurrent.*;

public class ConcurrentHashMapDemo {
    public static void main(String[] args) {
        ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
        map.put("Alice", 1);
        map.put("Bob", 2);

        // Concurrent updates
        map.compute("Alice", (k, v) -> v + 1);
        System.out.println(map);
    }
}
  

Explanation: Multiple threads can safely update the map without explicit synchronization.

Comparison Table

Aspect synchronized Keyword ConcurrentHashMap
Type Language keyword for mutual exclusion Thread-safe collection class
Granularity Locks entire method/block Segmented locking for buckets
Performance Can degrade under high contention Optimized for concurrent access
Use Case Protect critical sections Thread-safe map operations
Introduced Java 1.0 Java 5

Interview-Ready Notes

  • synchronized: Ensures mutual exclusion, simple to use, but can cause contention.
  • ConcurrentHashMap: Provides thread-safe map operations without global locking, better performance under concurrency.
  • Common Question: “Why use ConcurrentHashMap instead of synchronized HashMap?” → Because ConcurrentHashMap allows higher concurrency with segmented locks, avoiding bottlenecks.
  • Best Practice: Use synchronized for small critical sections; use ConcurrentHashMap for shared maps accessed by many threads.

Conclusion

Both synchronized and ConcurrentHashMap are essential tools in Java concurrency. synchronized is a fundamental keyword for mutual exclusion, while ConcurrentHashMap provides a high-performance, thread-safe collection. In interviews, emphasize their differences in granularity, performance, and use cases.

Why this Java question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.