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

Can you explain what a semaphore is and how it can be used in Java?

πŸ“• Buy this interview preparation book: 100 Java Concurrency questions & answers β€” PDF + EPUB for $5

In Java, a semaphore is a synchronization construct that is used to control access to a shared resource. A semaphore maintains a count of the number of permits that are available to access a shared resource, and threads can acquire or release permits to control access to the resource.

A semaphore can be used to enforce a limit on the number of threads that can access a shared resource at the same time, or to manage access to a resource that has a limited capacity. For example, a semaphore can be used to ensure that only a limited number of threads are accessing a database connection pool at any given time.

Here’s an example of how a semaphore can be used in Java:

import java.util.concurrent.Semaphore;

public class SemaphoreExample {
    private static Semaphore semaphore = new Semaphore(3);
    
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            Thread thread = new Thread(() -> {
                try {
                    semaphore.acquire();
                    System.out.println("Thread acquired semaphore");
                    Thread.sleep(1000);
                    semaphore.release();
                    System.out.println("Thread released semaphore");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            thread.start();
        }
    }
}

In this example, we define a semaphore with a permit count of 3, indicating that only 3 threads can acquire the semaphore at any given time. We then create 5 threads that each try to acquire the semaphore. When a thread acquires the semaphore, it prints a message to indicate that it has acquired the semaphore and then sleeps for 1 second. When the thread is finished, it releases the semaphore and prints a message to indicate that it has released the semaphore.

The output of this program will show that only 3 threads can acquire the semaphore at any given time, as specified by the permit count. The remaining threads will wait until a permit becomes available before they can acquire the semaphore.

Overall, semaphores can be a useful tool for managing access to shared resources in a multi-threaded application. By controlling access to shared resources, semaphores can prevent resource conflicts and improve the efficiency of the application.

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