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 7 of 100

What is a critical section in a multi-threaded application?

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

In a multi-threaded application, a critical section is a section of code that accesses shared resources, such as a shared variable or a shared data structure, and must be executed atomically by only one thread at a time. If multiple threads attempt to access the critical section simultaneously, race conditions and data inconsistencies can occur.

To ensure that critical sections are executed atomically by only one thread at a time, synchronization mechanisms such as locks or semaphores can be used. When a thread acquires a lock or semaphore, it enters a critical section, and other threads are prevented from entering the same critical section until the lock or semaphore is released.

Here’s an example of a critical section:

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 a critical section, as it accesses the shared counter variable and must be executed atomically by only one thread at a time. To ensure that the increment() method is executed atomically, we use the synchronized keyword to synchronize on the instance of the SharedResource class.

When a thread calls the increment() method, it acquires the lock on the instance of the SharedResource class, enters the critical section, and increments the counter variable. Other threads are prevented from entering the same critical section until the lock is released.

Overall, a critical section in a multi-threaded application is a section of code that accesses shared resources and must be executed atomically by only one thread at a time. Synchronization mechanisms such as locks or semaphores can be used to ensure that critical sections are executed atomically and prevent race conditions and data inconsistencies.

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