WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Concurrency · Basic · question 5 of 100

Why do we need to use synchronization in Java?

📕 Buy this interview preparation book: 100 Java Concurrency questions & answers — PDF + EPUB for $5

In Java, synchronization is used to ensure that multiple threads access shared resources in a safe and predictable manner. Without synchronization, multiple threads can access the same resource simultaneously, which can lead to race conditions and data inconsistencies.

Synchronization is typically used when multiple threads need to modify the same data or access the same resource, such as a shared variable or a shared data structure. By using synchronization, we can ensure that only one thread can access the shared resource at a time, preventing data inconsistencies and race conditions.

There are two ways to achieve synchronization in Java: using synchronized blocks and using synchronized methods.

Synchronized blocks: A synchronized block is a block of code that is synchronized on a specific object. Only one thread can execute the synchronized block at a time, preventing other threads from modifying the shared resource simultaneously. Here’s an example of a synchronized block:

public class SharedResource {
    private int counter;
    public void increment() {
        synchronized(this) {
            counter++;
        }
    }
}

In this example, we define a class called SharedResource that contains a counter variable. The increment() method is synchronized on the instance of the SharedResource class, using the synchronized(this) block. This ensures that only one thread can modify the counter variable at a time.

Synchronized methods: A synchronized method is a method that is synchronized on the object instance. When a thread calls a synchronized method, it acquires the lock on the object instance, preventing other threads from accessing the same object until the method completes. Here’s an example of a synchronized method:

public class SharedResource {
    private int counter;
    
    public synchronized void increment() {
        counter++;
    }
}

In this example, we define a class called SharedResource that contains a counter variable. The increment() method is declared as synchronized, which means that only one thread can execute the method at a time. When a thread calls the increment() method, it acquires the lock on the instance of the SharedResource class, preventing other threads from calling the method until it completes.

Overall, synchronization is necessary in Java to ensure that multiple threads access shared resources in a safe and predictable manner. By using synchronized blocks or methods, we can prevent race conditions and data inconsistencies, and ensure that our code is thread-safe.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Concurrency interview — then scores it.
📞 Practice Java Concurrency — free 15 min
📕 Buy this interview preparation book: 100 Java Concurrency questions & answers — PDF + EPUB for $5

All 100 Java Concurrency questions · All topics