Semaphore 00:00:00 Make a prediction You will separate counting capacity from identifying a resource and account for every acquired permit on both success and failure. A room has two printers and a semaphore initialized to two. Three people acquire without returning a permit. How many can begin printing? Two. The third waits or times out according to the API. The semaphore says how many may proceed, not which physical printer each person should use. 00:00:31 The hook board at the climbing wall Two climbing harnesses hang at the start of a wall. Each climber takes one before climbing and returns it on descent. Returning a second imaginary harness would let an extra climber proceed without equipment. A permit is a counted claim, not ownership metadata. Release once for each successful acquire, including exceptional exits. A semaphore holds a whole number of permits, zero or more. acquire waits until the number is above zero and takes one away, in a single step that no other thread can split. release adds one and lets a waiting thread continue. Many things exist in a fixed amount: connections, licences, graphics cards, memory for uploads, calls that a partner allows at once. Without a limit, a busy hour uses them up and everything fails together. 00:01:24 Give the third runner a turn A printer room has two printers. Runners A and B each acquire a permit. Runner C must wait because zero permits remain. When A finishes, returning exactly one permit allows one waiting runner to proceed. The semaphore tracks permitted concurrency, not which physical printer a runner owns. If the printing operation throws, a finally block must still return the permit. Test that subsequent runners can use both printers again. Start the first two runners and pause them inside the resource. The third waits. Release one permit and only then should the third begin. If a failure path releases twice, the count can rise above its starting value and a later run can violate the limit. 00:02:09 Pair capacity with resource selection A connection pool can use permits to bound concurrent borrowers and a protected collection to identify the individual connections. A permit alone cannot tell a borrower which connection is free. Return the actual connection to the collection before advertising capacity again. A permit confirms there is capacity. It does not identify which connection is available. Take a specific free object under the pool's own guard, then return the object before publishing capacity again. Reversing those steps advertises a connection that is still in use. A leaked permit shrinks capacity, and releasing without owning one inflates it. Test both under failure. Use a queue or pool alongside the semaphore when borrowers need distinct concrete resources rather than a simple concurrency limit. Java Semaphore. java.util.concurrent.Semaphore, since Java 5. Unfair by default, and its documentation says that barging is then permitted. Any thread may release, there is no ownership. Python threading and asyncio. threading has Semaphore and BoundedSemaphore, which raises ValueError when released too often. asyncio has both as well, for tasks on one event loop and explicitly not for threads. 00:03:29 Try an interview follow-up Can one thread deadlock with one semaphore? Yes. It holds the only permit and acquires again: there is no owner, so it waits for itself. Two threads can do it with two permits if each takes one and wants a second. Three tasks acquire three permits for a connection pool. One task times out after acquiring a permit but before getting a connection. Trace how the permit is returned exactly once in cleanup. Then cancel a task before it acquires anything and verify that cleanup does not create an extra permit. 00:04:06 What to take with you A semaphore is a counter of permits and a waiting line. Start it at 1 for one at a time, at N for a limit, at 0 for a signal. It has no owner, so the program must make sure that every acquire has exactly one release, best with a block or a guard object. It says how many, never which one, and never how many per second. A pool needs a lock beside it. A server needs a timeout, a short line and a decision about what the refused caller gets.