Building thread-safe abstractions in Java versus Go
[^1]: Go's built-in map type isn't concurrent safe. There's a [`sync.Map`](https://pkg.go.dev/sync#Map) type that can be used as an alternative, but it's generally not recommended unless you have done performance testing to confirm it's faster since it adds overhead in some scenarios. [^2]: The way `wait()` works in Java is it pauses the current thread, only waking it up once until either (a) a timeout expires, or (b) the thread is woken up by a call to `notify()` or `notifyAll()`. [^3]: Specifically, `Object.notifyAll()` it wakes up all threads that went to sleep via other `Object.wait()` calls made by the same object. [^4]: Alas, this implementation isn't the most efficient, since multiple threads may be woken up by `notifyAll()`, but it's possible only one of them can re-acquire the lock. This isn't too hard to fix, just requires a little bit of extra bookkeeping. [^5]: They are also tricky to use properly. I really like the mental model of channels provided in [this](https://www.dolthub.com/blog/2024-06-21-channel-three-ways/) blog post, if you're interested in learning more about them. [^6]: This algorithm can be optimized a bit further to reduce how many goroutines are woken up in high-concurrency scenarios, but at the very least this gets the implementation working.
functional fascinations