User Tools

Site Tools


Sleeping Barber - Java Threads Problem

The Sleeping Barber problem is another classic synchronization problem. It involves a barber shop with a barber, a barber chair, and a waiting room with a number of chairs in it.
If there are no customers, the barber goes to sleep. If a customer arrives and the barber is asleep, they wake the barber up.
If the barber is busy but chairs are available in the waiting room, customers sit in a chair and wait. If the barber is busy and no chairs are available, customers leave.
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class SleepingBarber {
    // Semaphore representing the number of chairs in the waiting room
    private static final Semaphore WAITING_ROOM = new Semaphore(4);
    // Semaphore to determine whether the barber is available
    private static final Semaphore BARBER = new Semaphore(1);
    static class Customer implements Runnable {
        private final int id;
        Customer(int id) {
   = id;
        public void run() {
            try {
                if (WAITING_ROOM.tryAcquire()) {
                    System.out.println("Customer " + id + " is waiting.");
                    WAITING_ROOM.release(); // Leave the waiting room chair
                    BARBER.release(); // Release the barber
                } else {
                    System.out.println("Customer " + id + " leaves (no chairs available).");
            } catch (InterruptedException e) {
        private void getHaircut() throws InterruptedException {
            System.out.println("Customer " + id + " is getting a haircut.");
    public static void main(String[] args) throws InterruptedException {
        for (int i = 1; i <= 10; i++) {
            new Thread(new Customer(i)).start();

Semaphores: Two semaphores are used - WAITING_ROOM to represent the chairs in the waiting room and BARBER to represent the barber's availability.

Customer Class: Represents customers who arrive at the barber shop. Each customer tries to acquire a waiting room chair (WAITING_ROOM semaphore). If successful, they then wait for the barber (BARBER semaphore).

Getting a Haircut: Once the barber (semaphore) is available, the customer gets a haircut, represented by Thread.sleep.

Leaving the Shop: If there are no chairs available in the waiting room, the customer leaves. After the haircut is done, the customer releases the barber semaphore.

Main Method: Simulates the arrival of customers at random intervals. For each customer, a new thread is started. If the waiting room is full (all semaphores for chairs are taken), new customers will leave. Otherwise, they wait for their turn with the barber.

This implementation models the Sleeping Barber problem in a concurrent setting using semaphores for resource management.

The WAITING_ROOM semaphore controls access to the waiting room chairs, ensuring that no more customers than there are chairs can wait.

The BARBER semaphore ensures that only one customer at a time gets a haircut. This setup effectively prevents race conditions and manages the synchronization between customers and the barber.

java/threads/sleeping-barber.txt · Last modified: 2024/01/17 03:18 by odefta