Examples

Page 35 of 50

Browse code examples

goSnippet

Sync.Map LoadOrStore And Range

Guard a shared cache with sync.Map: LoadOrStore inserts atomically when absent, Load reads with comma-ok, and Range walks entries without blocking writers.

goSnippet

Ticker Periodic Tasks With Stop

Drive periodic maintenance with time.NewTicker, consume ticks in a select loop alongside a stop channel, and always Stop the ticker so timers do not leak.

javaSnippet

Exchanger Swap Between Two Threads

Meet another thread at an Exchanger and trade objects in one atomic handoff — exchange() blocks until both sides arrive, then returns the partner object.

javaSnippet

LinkedHashMap Access-Order LRU

Bound a cache with LinkedHashMap in access order: get refreshes recency and removeEldestEntry evicts the least recently used key once capacity is exceeded.

javaSnippet

StampedLock Optimistic Read

Take an optimistic snapshot with tryOptimisticRead and validate the stamp; fall back to a real read lock only when a write interfered mid-read.

javaSnippet

StringJoiner Delimited Output

Assemble delimited output with StringJoiner: prefix and suffix framing, an explicit empty value, and merge that reuses the first joiner's delimiter.

javaSnippet

ThreadLocal Per-Thread State

Give each thread its own counter via ThreadLocal.withInitial so mutable per-thread state needs no locking; clear it before pooled threads get reused.

pythonSnippet

ExitStack Unwinds Dynamic Cleanup

Clean up dynamic resource sets with contextlib.ExitStack: enter_context registers handles, callback registers plain functions, and unwinding runs LIFO.

pythonSnippet

deque maxlen Keeps Recent Events

Track recent events with a bounded deque: maxlen evicts the oldest end automatically, both ends are O(1), and rotate shifts the whole window in place.

rustSnippet

Match Io ErrorKind For Recovery

Classify io failures by ErrorKind instead of string matching: fall back to defaults on NotFound, refuse on PermissionDenied, and surface anything else loudly.

sqlSnippet

Create Index CONCURRENTLY

Build an index while the table stays writable with CREATE INDEX CONCURRENTLY, then clean up the INVALID leftovers a failed build can leave.

sqlSnippet

Fillfactor And HOT Updates

Leave free space on pages with fillfactor so updates that touch no indexed column stay on-page as HOT updates and skip index maintenance entirely.