Stately icon indicating copy to clipboard operation
Stately copied to clipboard

Concurrent and Synchronized are two different things: This is synchronized, not concurrent

Open Zomis opened this issue 1 year ago • 1 comments

I was looking for a replacement for Java's ConcurrentHashMap in Kotlin and stumbled upon this library.

When reading the code https://github.com/touchlab/Stately/tree/main/stately-concurrent-collections/src/commonMain/kotlin/co/touchlab/stately/collections I was a bit disappointed and would like the repository to clarify a few things or improve things.

Currently, these classes are not concurrent, they are synchronized.

Concurrent classes allows scaling and would actually allow multiple threads to make changes to the same collection. This is made possible by not synchronizing on the whole collection, but on individual "buckets" inside the collection. Synchronized classes does not scale and only allows one thread to make changes.

I would recommend to clarify this by changing the names (although that would be a breaking change, so maybe use a deprecated typealias to support backwards compatibility) and adding to the readme that the collections use synchronization on a single object to make them thread-safe, and that they are not concurrent.

An even better solution would however of course be to make the collections properly concurrent.

For more information, see e.g. https://javarevisited.blogspot.com/2016/05/what-is-difference-between-synchronized.html#axzz8dav8KUva and https://www.baeldung.com/java-synchronizedmap-vs-concurrenthashmap

Zomis avatar Jun 19 '24 11:06 Zomis

this fact brings issues like https://github.com/touchlab/Stately/issues/105

lukellmann avatar Jun 30 '24 15:06 lukellmann