Skip to content
LLD Learn/Concurrency for LLD
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Concurrency-Safe Patterns

4 min read

You'll learn to

  • -Explain when a Builder needs thread-safety (when it is shared) and the simple fix (do not share it)
  • -Build a connection pool that makes extra callers wait, and watch it never exceed its size
  • -Explain why release belongs in a finally block, and weigh coarse-grained against fine-grained locking

Most patterns from earlier chapters are safe under concurrency without extra work, because they produce results that never change or objects that hold no state. Two need explicit thought when several threads use them at once. They are the ones interviewers raise most often as follow-ups.

Builder Under Concurrency: Usually a Non-Issue, With One Exception

A builder constructs a complex object step by step, such as adding toppings to a pizza order before you finally call build(). A builder used by one thread from start to finish needs nothing special. It is local state that never leaves that thread. The exception is one builder shared between threads. Think of two people scribbling on the same order sheet at the same time. The toppings from the two interleave unpredictably. The fix is almost always simpler than a lock: do not share builders. Each thread makes its own, and only the finished, unchangeable result is shared.

Object Pool: Reusing Expensive Objects Safely

An object pool keeps a set of ready-made, reusable objects, such as database connections, instead of building and destroying one for every use, since building them is expensive. Think of a bike-share dock with only a few bikes. You take one, ride, and bring it back. If the dock is empty, you wait until someone returns a bike.

Under concurrency the pool itself is a shared, changeable resource, which is exactly the shape from the previous chapter. Taking from it and returning to it must be thread-safe. The demo uses a lock plus a condition, a tool that lets a thread wait until another thread says "something changed". The pool has two connections, but four workers want one each at the same time.

Press Run: four workers share two connections, and the extra two simply wait

Notice that this is the same wait-and-notify mechanism as the bounded queue in the previous chapter. The pool just holds reusable objects instead of work items. Once you recognize the shape, the same few tools solve many problems.

Coarse-Grained vs. Fine-Grained Locking

Locking can be coarse or fine. One lock around the whole pool is coarse-grained: simple and safe, but every operation queues behind it. Several smaller locks, each guarding a small piece, are fine-grained: faster under heavy load but much easier to get wrong, for example by locking two things in opposite orders and freezing the program. Start coarse, and move to fine only when you can name the bottleneck.

A common pool bug: forgetting to release the object when the work throws an error. A try/finally (or a context manager) around acquire and release guarantees the release. A leaked connection permanently shrinks the pool.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!