Kotlin has mutexes that presumably are compatible with coroutines:
kotlinx.coroutines.sync.Mutex
Until Java sorts this out, it could be its "Rust async" watershed moment.Kotlin has mutexes that presumably are compatible with coroutines:
kotlinx.coroutines.sync.Mutex
Until Java sorts this out, it could be its "Rust async" watershed moment.Like, it makes sense that when you synchronized on an object, there's room for contention. It seems weird that synchronizing on an _uncontended_ object can cause contention. But that's what this is. The behaviour is that you have target numCores carrier threads, and if someone synchronizes on an object and then does a non-blocking sleep, it's now blocking, because synchronizing upgraded the non-blocking I/O to blocking.
So basically when you hit this issue, it's because not only has the bad thing been happening, it's also been happening badly enough that all the compensation mechanisms have failed.
It's just weird that a whole language level keyword behaves this badly.
But people just… don’t.
Where has this been communicated? The first I heard that you shouldn't use synchronized statements or blocks was in these virtual threads discussions; and the popular IDE I use for Java at work doesn't flag synchronized methods or blocks as something to be avoided. The only thing I had heard about it was that you should avoid synchronizing on objects exposed to users of your library, since it would make it possible for code outside your control to also synchronize on that same object (but IMO synchronizing on arbitrary objects you got from somewhere is itself a bad practice, you should only synchronize on objects you control). And obviously, synchronized methods or blocks do not have the "fair" mode, which might be necessary (or good) in some cases.
(And before Java 9, stack overflows within the implementation of some concurrency constructs could lead to unexpected behavior, see https://openjdk.org/jeps/270 for details; this was never a problem with synchronized methods or blocks.)
There of course are things like ErrorProne that statically check that you unlocked your lock, but there's still bugs possible.