Having a sane built-in concurrency model is definitely a big plus for a language. That's definitely one of the things where go excels at.
I do like future-based async code, because it tends to be very clean and testable. Java's implementation however, is a big mess with very questionable design choices.
Here's a quick draft in scala, that intention wise is very close to your Java code, but using Scala's Future.sequence and Future.firstCompletedOf, avoiding low level concurrency completely.
If I were forced to implement this in Java, I would implement sequence (using allOf) and firstCompletedOf (using anyOf) instead of using concurrent datastructures.
Thanks for writing down the Scala way.
I have not written any significant piece of code in Scala, but every time I stumble in something written in it, it looks just so elegant.
You made me think I should learn more about it :)
By the way, your scala code would make a great response post :)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Nice comparison!
Having a sane built-in concurrency model is definitely a big plus for a language. That's definitely one of the things where go excels at.
I do like future-based async code, because it tends to be very clean and testable. Java's implementation however, is a big mess with very questionable design choices.
Here's a quick draft in scala, that intention wise is very close to your Java code, but using Scala's Future.sequence and Future.firstCompletedOf, avoiding low level concurrency completely.
If I were forced to implement this in Java, I would implement sequence (using allOf) and firstCompletedOf (using anyOf) instead of using concurrent datastructures.
I think you can write the third function in an even shorter way with
Future.traverse
instead ofFuture.sequence
:I didn't expect this, but I discovered that
Future.traverse
is really handy in lots of situations.Thanks for writing down the Scala way.
I have not written any significant piece of code in Scala, but every time I stumble in something written in it, it looks just so elegant.
You made me think I should learn more about it :)
By the way, your scala code would make a great response post :)