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

Java Concurrency · Intermediate · question 21 of 100

How does thread synchronization work in Java?

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

Thread synchronization is a mechanism in Java that ensures that two or more threads do not execute certain sections of code simultaneously, which can lead to race conditions and other synchronization problems. This is accomplished through the use of locks, which allow threads to control access to shared resources.

In Java, there are several ways to synchronize threads, including the use of synchronized blocks, synchronized methods, and atomic variables.

Synchronized Blocks: A synchronized block is a section of code that is executed by only one thread at a time. It is defined using the synchronized keyword, followed by a reference to the object that should be used as the lock.

Here’s an example of how to use a synchronized block in Java:

public class SynchronizedBlockExample {
    private int counter = 0;
    
    public void incrementCounter() {
        synchronized(this) {
            counter++;
        }
    }
}

In this example, we define a class called SynchronizedBlockExample with a counter variable that will be accessed by multiple threads. The incrementCounter() method is synchronized using a synchronized block that uses the "this" keyword as the lock. This ensures that only one thread can increment the counter variable at a time.

Synchronized Methods: A synchronized method is a method that is executed by only one thread at a time. It is defined using the synchronized keyword in the method declaration.

Here’s an example of how to use a synchronized method in Java:

public class SynchronizedMethodExample {
    private int counter = 0;
    
    public synchronized void incrementCounter() {
        counter++;
    }
}

In this example, we define a class called SynchronizedMethodExample with a counter variable that will be accessed by multiple threads. The incrementCounter() method is synchronized using the synchronized keyword in the method declaration. This ensures that only one thread can increment the counter variable at a time.

Atomic Variables: An atomic variable is a variable that can be accessed by multiple threads without the need for synchronization. This is because the operations performed on an atomic variable are guaranteed to be atomic, meaning they will be executed as a single, indivisible operation.

Here’s an example of how to use an atomic variable in Java:

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicVariableExample {
    private AtomicInteger counter = new AtomicInteger(0);
    
    public void incrementCounter() {
        counter.incrementAndGet();
    }
}

In this example, we define a class called AtomicVariableExample with a counter variable that will be accessed by multiple threads. The counter variable is defined as an AtomicInteger, which is an atomic variable that can be incremented atomically using the incrementAndGet() method. This allows multiple threads to increment the counter variable without the need for synchronization.

Overall, thread synchronization is an important mechanism in Java that allows multiple threads to access shared resources without the risk of synchronization problems. By using locks, synchronized blocks, synchronized methods, and atomic variables, developers can ensure that their multi-threaded applications are thread-safe and efficient.

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