5 ms·
Well, the first two lines are usually written somewhere at the top level. You won't actually write that anytime you want something to be async. The last two lin
by Skinney 6y ago
Well, the first two lines are usually written somewhere at the top level. You won't actually write that anytime you want something to be async. The last two lines are what you usually write, so most of the time, the number of lines of code are equivalent.
Second, Java doesn't have async/await today. If Java was to introduce it, it wouldn't be compatible with the code written today. The big benefit of project loom is that code written today will get all the benefits of async/await without changing any code. In fact, because it's not important to pool threads, it actually gets easier.
The big problem with async/await in languages like C# or even coroutines in Kotlin is that it doesn't mix with the "old" api. The get the benefits you need to do a huge refactor of your code, and you need to make sure that any library you pull in is compatible.
Java's approach seems much better.
- jayd16 6y agoYou don't. You can call synchronous functions from async functions just fine and vice versa. The syntax isn't identical but I don't understand this split the world meme.
- toast0 6y agoIn my experience with C#, I ran into apis I wanted to call that were async. I couldn't call them, even as Result foo = await async_thing(), unless my function was marked as async. I don't remember having a problem calling sync functions from async functions though.
- uryga 6y agoafaik sync can call async if the language allows you to make an executor/event-loop. e.g. with python's asyncio it'd be something like def sync_foo(): task = async_bar() loop = asyncio.new_event_loop() return loop.run_until_complete(task) after all, unless the loop is built into the language (like JS), you need bootstrap the async code somehow
- jayd16 6y agoYou can just use: task.RunSynchronously() mildly annoying but far from impossibly split.
- Skinney 6y agoBut now you’re blocking the thread, so if this function is being used by an async function higher up in the function chain, it’s no longer async (unless that is changed as well. There’s a split, because every step of an async process has to be async/await compatible or you lose all benefit. In a small code base, this might not be a big problem. Where I work it will never realisticly be done. With project loom we won’t have to do anything or learn anything new. Just upgrade the java runtime and everything just works better :)
- J-Kuhn 6y agoAnd use a different executor. But yeah, the magic is: All code is now implicitly async (except native calls) - as if all code always had async in it's signature. Just - there is no need to now add this to the signature.