Andrew Ducker (andrewducker) wrote,
Andrew Ducker
andrewducker

I am not enjoying writing threaded code in Java

Every time I use Java I find myself wondering whether the people in charge of its libraries are determined to make everything harder than necessary.

In JavaScript I can use promises and chain code together like so:

http.fetchSomething(myUrl)
.then(function(result){return saveTheResultAsynchronously(result);})
.then(function(furtherResult){displayTheResult(furtherResult);});

And provided the various methods return promises each "then" will be called once the previous bit of functionality has finished running, allowing them to be chained together really easily.

C# uses async/await for make things easy too.

Java, on the other hand, has "Future" as a type, which is the equivalent of a JS Promise - but one that doesn't allow for callbacks. Your options are to either poll "is it done yet?" repeatedly or to block the thread. There's no equivalent of "Then", which makes it really hard to run code when something finishes, let alone chain asynchronous methods together one after another.

You'd think that "Do X, and then when X is done, do Y" would be a basic requirement for any kind of asynchronous processing, but apparently the Java library creators disagree...



Original post on Dreamwidth - there are comment count unavailable comments there.
Subscribe

  • Interesting Links for 01-03-2021

    A Decades-Long Quest Reveals New Details of Antimatter (tags: physics ) Which character from Hieronymus Bosch's Garden of Earthly Delights are…

  • Interesting Links for 28-02-2021

    Single Covid case puts New Zealand's largest city Auckland back into lockdown (tags: pandemic newzealand ) Do you like it when things in your…

  • Interesting Links for 27-02-2021

    Covid in Scotland: 40-49 next to be vaccinated (including me!) (tags: vaccination Scotland pandemic age ) Cairngorms recreated in Minecraft…

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 8 comments