Hacker News new | ask | show | jobs
by vips7L 2117 days ago
And now Java is benifiting from being a second mover on a lot of the features C# adopted.

See virtual threads vs async/await. Java's version is going to be better.

1 comments

I always end up with a couple of Task.Run() calls, because cannot change the complete call chain to deal with async/await.
If you want to avoid changing the complete chain to Task, there’re workarounds.

You can block the caller thread waiting for the task. Deadlocks are possible with this approach due to synchronization context shenanigans, but they’re easy to fix, VS debugger is quite good at multithreading.

Another method, you can run whatever dispatcher on your main thread, in modern .NET that’s usually Dispatcher.PushFrame.

Also, if the async method returns no values, you don’t need to change the call chain, you just need to catch and handle exceptions.

Even that situation, you can still use await inside Task.Run, it should be useful than nothing.