bowbahdoe / mccue.dev-comments

0 stars 0 forks source link

pages/5-2-22-go-concurrency-in-java #5

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

Go's Concurrency Examples in Java 19

https://mccue.dev/pages/5-2-22-go-concurrency-in-java

LifeIsStrange commented 2 years ago

Excellent post, I would be curious about a comparison of goroutines vs Kotlin coroutines too.

bowbahdoe commented 2 years ago

@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.

LifeIsStrange commented 2 years ago

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

bowbahdoe commented 2 years ago

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

bowbahdoe commented 2 years ago

/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.

alexismanin commented 2 years ago

@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 :-)