#concurrency Clear
Tags #php #kotlin #bash #go #sql #rust #typescript #html #java #python #files #utils #strings #http #concurrency #async #json #arrays #security #types #crypto #database #dates #format
Java ExecutorService — Thread Pools
`ExecutorService` is the modern way to manage threads. Submit Callables/Runnables, get Futures back. Always shutdown the executor — and prefer try-with-resources on Java 19+ where ExecutorService is AutoCloseable.
JavaScript Queue (Async Task Queue)
A serialised async task queue that executes queued async functions one at a time, in order. Useful for sequencing operations that must not run concurrently (file writes, ordered API mutations, step-by-step wizards). add() returns a Promise that resolves/rejects when that specific task completes.
JavaScript Promise All with Concurrency Limit
Runs an array of async tasks with a maximum concurrency cap, preventing you from firing hundreds of requests simultaneously. Processes items in batches, moving to the next as slots free up. Essential when hitting rate-limited APIs or processing large datasets without overwhelming the server.
JavaScript Worker Pool (Web Workers)
Manages a pool of Web Workers for CPU-intensive tasks (image processing, crypto, parsing). Distributes tasks across available workers using a round-robin strategy and returns a Promise per task. Falls back gracefully to inline execution in environments where workers are unavailable.
Go sync.Mutex vs sync.RWMutex
`Mutex` allows one goroutine at a time — for both reads and writes. `RWMutex` allows multiple concurrent readers OR one writer — much better when reads dominate.
Go select — Multiplex Channel Operations
`select` is like a switch for channels — runs whichever case is ready. Use `default` for non-blocking sends/receives; `case <-time.After(d)` for timeouts.
Go Worker Pool with Channels
Spawn N worker goroutines, feed them tasks over a jobs channel, collect results on a results channel. Idiomatic Go pattern for bounded concurrency.
Go Channels — Basics
Channels are typed pipes between goroutines. Unbuffered = synchronous handoff; buffered = up to N values queued. Close a channel to signal "no more values" — receivers see ok=false.
Go Fan-Out / Fan-In
Distribute work across N workers (fan-out), then merge their results back into one channel (fan-in). Used when you can't process items strictly in order but want to retain output as one stream.
Rust Atomic Counter (lock-free)
For shared counters and flags, `AtomicU64` / `AtomicBool` etc. are much faster than `Mutex`. Operations like `fetch_add` are single CPU instructions on most architectures.
Java Semaphore — Bounded Concurrency
Limit how many threads can enter a critical section. `acquire()` blocks until a permit is available; `release()` returns one. Use for rate limits, connection pools, or any "max N at a time" pattern.
Go errgroup — Fan-Out with Cancellation
`golang.org/x/sync/errgroup` runs N goroutines, returns the first error, and cancels the others via a shared context. The right pattern for parallel I/O where any failure should abort the rest.
TypeScript Concurrency Limiter (semaphore)
Run an array of async tasks but limit concurrency to N at a time — like p-limit, in 30 lines. Preserves input order in the output array.
Go context.Context — Cancellation & Deadlines
`context.Context` propagates deadlines, cancellation, and request-scoped values down a call chain. EVERY blocking / long-running function should take a `ctx context.Context` as its first parameter.
Go Goroutine Leak Prevention
A goroutine that's blocked forever on a channel send/receive leaks — it's never garbage-collected. Always pair channels with `select { case <-ctx.Done(): return }` to give them an exit path.
Go Pipeline (channel chain)
A pipeline is a chain of stages connected by channels — each stage runs in its own goroutine. Classic Go pattern for streaming transformations with backpressure built in.
Go sync.Pool — Reuse Allocations
`sync.Pool` keeps a pool of reusable objects to reduce allocator pressure in hot paths. Good for byte buffers, JSON encoders, or anything you allocate frequently and only need briefly.
Python asyncio Concurrency Limiter (Semaphore)
Run hundreds of coroutines but cap how many are in-flight at any moment. asyncio.Semaphore inside the task body is the cleanest way to "do at most 10 of these at a time."