HotSpot mark word layout and lock states (unlocked, lightweight, heavyweight, GC mark)

Concurrent Hash Table Designs: Synchronized, Sharding, ConcurrentHashMap, and NonBlockingHashMap

Discussions Hacker News Reddit The next milestone is to build a fully thread-safe hash map. Up to this point, the focus has been entirely on single-threaded performance: minimizing memory overhead, improving cache locality, and squeezing out every last bit of throughput from the underlying data layout. However, real-world applications rarely stay single-threaded. To be practically useful, a hash map must behave correctly—and efficiently—under concurrent access. Before jumping straight into implementation, it’s worth stepping back and studying how existing thread-safe hash map implementations approach this problem. Different designs make different trade-offs between simplicity, scalability, memory usage, and read/write performance. By examining these approaches side by side, we can better understand which ideas scale cleanly—and where the pitfalls are—when multiple threads hit the same structure at once. ...

December 27, 2025 · 50 min · Donghyung Ko

Spinlock vs Mutex

Locks split into two broad families: spinlocks, which run entirely in userspace on raw CPU instructions, and mutexes, which lean on the kernel. Both exist to guarantee mutual exclusion, but they get there differently, and that difference determines which workload each one fits. Spinlock A spinlock lives entirely in userspace. The mechanism is simple: loop forever until a CAS (Compare-And-Swap) succeeds. while (!CAS(lock, 0, 1)) Most CPUs ship a dedicated instruction for this. On x86 it’s LOCK CMPXCHG. The catch is that a CAS only succeeds once the core holds exclusive write access to that cache line. Getting there pulls the cache coherence protocol into play, and passing ownership of a cache line back and forth triggers frequent invalidation, what people call cache line bouncing. That round trip usually costs somewhere between 4 and 80 nanoseconds. ...

December 12, 2025 · 4 min · Donghyung Ko

Kotlin Coroutine Internals: Suspension, Continuation, CPS

This post explains how coroutines work, referencing the design proposal Kotlin Proposals - Coroutines. Coroutine The proposal describes a coroutine in one sentence as an instance of suspendable computation. The essential trait of a coroutine is its ability to suspend. So what exactly does “suspendable” mean? Suspension According to the proposal, suspendable means a coroutine can pause execution on the current thread, yield the thread so another coroutine can run, and later resume, possibly on a different thread. ...

July 7, 2023 · 8 min · Donghyung Ko