AlgoViz

Concurrency in an LLD round

medium 40 min

Sooner or later the interviewer asks what happens when two users do it at the same time.

Watch lesson5:07

Where it shows up

The same pattern appears across everyday software.

  • Jakarta Servlet: one object, many requests

    The specification says that a container may send concurrent requests through the `service` method, so developers must design servlets for several threads. A request field in a servlet is the classic hidden sharing.

  • Android: one thread owns the screen

    The documentation says the UI toolkit is not thread-safe: do not touch views from a worker thread, do it from the UI thread. Correctness by giving the data a single owner.

  • Java BlockingQueue: coordination in a class

    Its documentation describes operations that wait for the queue to become non-empty when taking and wait for space when storing.

  • Java Semaphore: scarcity in a class

    The documentation says semaphores are often used to restrict the number of threads that can access a resource, and shows a pool of items as its example.

  • HikariCP: a pool with a top

    The connection pool has `maximumPoolSize`, counting idle and used connections, and a time limit for waiting. Both answer the scarcity question: how many, and what then?

  • Go: share by communicating

    The Go blog repeats the advice: do not communicate by sharing memory, share memory by communicating. A value is handed over, so one goroutine owns it at a time.

  • Node.js: do not block the event loop

    Its guide explains that few threads serve many clients, so one slow handler keeps the others waiting. There is no lock, and the scarce thing is the loop itself.

  • SQLite: the mode is a design choice

    It documents three threading modes: single-thread without mutexes, multi-thread, and serialized, the default, which puts access to each object in a row with mutexes.