Skip to content

Is it possible to implement a semaphore that avails permits in timed intervals?

I’m trying to implement a program that allows a maximum of 5 customers to enter a store at once. They move around (one block at a time, max 1 customer per block) and then exit. The problem I’m facing is that my semaphore, with 5 permits, throws them all at the entrance block all at once which then violates one of my conditions (Only 1 customer per block). This is how I handled entrance

public synchronized GridBlock enterShop() throws InterruptedException  {
    GridBlock entrance = whereEntrance();
    return entrance;


If there’s a limit of one customer per “block” then the straightforward solution is one semaphore per block, with limit 1. When a customer moves on, you have to first “down” the sema for the block being entered, and “up” the sema for the block being left.

If you have more than 5 blocks but there’s still a limit of 5 customers total. then you also need the entrance sema.That would be “downed” for entering the first block, and “upped” for leaving the last one.

Using a single semaphore (limit 5) per all blocks, and no other, implicitly imposes no other restrictions.