Examples

Page 7 of 50

Browse code examples

javaSnippet

Lock Ordering Avoids Deadlock

Order lock acquisition by account id so opposite-direction transfers queue on the same locks instead of holding one and waiting for the other forever.

javaSnippet

PlusMonths Month-End Clamping

plusMonths and plusYears clamp to the last valid day instead of overflowing into the next month, and the clamp is not reversible when months are added back.

javaSnippet

Poison Pill Queue Shutdown

End a consumer loop by sending a sentinel object through the queue: the producer appends it after the last job and the consumer breaks on identity.

javaSnippet

Private Final Lock Object

Guard mutable list state with a private final lock object instead of synchronized(this) so outside code cannot interfere with the monitor.

javaSnippet

Record Overloaded Constructors

Add secondary record constructors that delegate to the canonical one, deriving checkout dates and parsing ISO strings without breaking component accessors.

javaSnippet

Parse Fixed Width Column Text

Extract fields from fixed-width records with per-column start and end offsets, keeping short lines honest instead of throwing past the line end.

javaSnippet

Token Bucket Rate Limiter (Java)

Refill tokens continuously from elapsed time and admit only when one is available, so bursts up to capacity pass while steady traffic stays capped.

javaSnippet

UUID Version 7 Time Ordered

Build RFC 9562 version-7 UUIDs from a 48-bit millisecond timestamp so lexicographic order matches creation time, then verify compareTo follows the clock.

javaSnippet

Zip Two Lists Pairwise

Zip two lists by index into record pairs: the shorter side caps the length so uneven inputs clip instead of throwing IndexOutOfBounds.

javascriptSnippet

Event Loop Delay Histogram

Record how late timer callbacks run with monitorEventLoopDelay: a synchronous burst blocks the loop and shows up as nanosecond-scale max delay.