Open utterances-bot opened 2 years ago
Excellent post, I would be curious about a comparison of goroutines vs Kotlin coroutines too.
@LifeIsStrange So would I. I don't feel like I know enough about Kotlin coroutines to pull that off though.
There would need to be some channel with a coroutine-yielding put to be faithful to the concept. If you know of an implementation I can give it a try.
I don't know a specific implementation and I'm too lazy to do a blog (also it's been a while tje last time I read in depth about coroutines) but since kotlin explicitly support channels, I suppose a direct translation of the go code would kinda make sense (although not necessarily the most idiomatic approach given that Kotlin has bonus concepts and APIs such as Flow. https://kotlinlang.org/docs/channels.html
Here's the guide BTW https://kotlinlang.org/docs/coroutines-guide.html
Good notions to explore where Kotlin concurrency innovate are: Flow Cancellability And most importantly: structured concurrency
It has been pointed out on reddit by /u/casualsuperman that the proper class to use as an equivalent to an unbuffered chan would be a SynchronousQueue
or a TransferQueue
.
On a quick skim of the docs, I think the SynchronousQueue
with fairness set to true
is the more directly applicable but I might be wrong.
I don't think this affects the "correctness" of any of the examples, but there is a difference in behavior where a thread will put to a queue and move on in Java and in Go that thread would instead wait until the value is consumed.
Learning lots of things here folks
/u/Beaverman on reddit and ibraheemdev on hacker news + others have pointed out that my initial discussion of thread stack spaces was inaccurate.
Previously I thought the full ~megabyte was given at thread inception. It seems that does not happen until it outgrows an ~4kb initial stack space.
I updated with what I think is a more accurate (but still hand wavey) explanation.
@LifeIsStrange and @bowbahdoe : If you want a glimpse over coroutines, I've reproduced the examples of the article with it in this repo: https://github.com/alexismanin/concurrency-examples-kt
I think there's still room for improvement in my code, but it is a good start for comparison. The repository also contains another Spring Reactor based implementation because... I like it :-)
Go's Concurrency Examples in Java 19
https://mccue.dev/pages/5-2-22-go-concurrency-in-java