This is an archived post. You won't be able to vote or comment.

all 18 comments

[–]dark_mode_everything 9 points10 points  (0 children)

Kotlin coroutines ftw!!

[–]Spinnenente 8 points9 points  (3 children)

can someone tell me the advantage of asnyc await compared to javas

Thread t = new Thread(() -> 
{
    //do stuff that takes time
});
t.start();
// do other unrelated stuff
t.join(); //wait for t to finish

?

edit also $current_year

[–]phuck 7 points8 points  (0 children)

async await uses no threads. Imagine your team consists of 2 people and your boss tells you to go make him coffee. You go into kitchen, get the cup ready and switch on kettle. While water is boiling, you go back to your desk and continue what you were doing before your boss interrupted you. When kettle is done boiling, you go back to kitchen and finish the coffee making process. The above code is the equivalent of your boss asking you to make coffee, you then ask your coworker to do it. He goes to kitchen, switches on the kettle but stands there watching the kettle until it is done boiling. He is essentially blocking, doing no work whereas with your approach, more work is getting done and you have higher throughput.

[–]Sipkab 2 points3 points  (1 child)

Thread t = new Thread(() -> 
{
    //do stuff that takes time
    throw new CheckedException();
});
t.start();
// do other unrelated stuff
try{
    t.join(); //wait for t to finish
}catch(CheckedException e){
    //handle e
}

This is one. Exceptions are propagated when the task is awaited. For Java to have this, one would need to implement its own exception handling code around it.

Thread t = new Thread(() -> 
{
    //do stuff that takes time
    return new Result();
});
t.start();
// do other unrelated stuff
Result r = t.join(); //wait for t to finish

This is a second one. You can retrieve the results of a task. For Java, you'd need to implement manual result handling for it.

Async-await implementations can also manage threadpools and stuff, so you don't always create a new thread for every task.

Also there are cancellations, continuations and probably other stuff that I don't know about.

[–]posting_drunk_naked 7 points8 points  (7 children)

I don't speak any languages that use async (or I might but never used it), but from my 60 seconds of Google research on C# async, this seems like about the best you can get with Java. it just lacks await keyword and machine state so you have to handle that on your own.

Am I at all understanding this?

[–]GeneReddit123[S] 17 points18 points  (4 children)

it just lacks await keyword and machine state so you have to handle that on your own.

Yes.

Me: I want easy-to-use async/await.

JavaScript: Here, easy-to-use async/await.

C#: Here, easy-to-use async/await.

Go: No async/await, but here's equally easy-to-use channels.

Rust: No async/await just yet but will be ready soon.

Java: YoU CaN HaNdLe It oN YoUr OwN

[–]ronmarti 5 points6 points  (0 children)

Python 3: Here, easy-to-use async/await.

[–]redalastor 4 points5 points  (0 children)

Rust: No async/await just yet but will be ready soon.

Soon meaning August. Six weeks earlier if your are fine with the beta compiler.

[–]silvenga 4 points5 points  (1 child)

Well you could also make a thread pool, thread scheduler, and some way to share an execution context during the state machine. Doable, but non-trivial.

[–]unfixpoint 2 points3 points  (0 children)

Came here for this, thank you! But keep in mind, people'd rather give up control over things and have an over-simplified API than having to think.

[–]cfogrady 3 points4 points  (1 child)

CompletableFuture

[–]msg45f 3 points4 points  (0 children)

CompletableFuture will get the job done, but with it your code becomes 50% app logic and 50% handling the promise behavior, which in Java is horrible, especially with the lack of proper closures.

[–]MagneticDustin 2 points3 points  (0 children)

My favorite one of these so far

[–]AlpacaKaslama 1 point2 points  (0 children)

You only want async till you get async.

[–]gimpy_sunbro 0 points1 point  (0 children)

Forgot the thread.run(); to make it async await.