I read some stack overflow answers about sync, async, and threading on post A and post B. The main point I got from the post is “multithreading is only one kind of asynchrony. Threading is about workers; asynchrony is about tasks” from post A and “Technically, the concept of synchronous/asynchronous really does not have anything to do with threads” from post B
I believe this is general idea in programming world. so what I wonder about this is “is it true in android(java/kotlin)? if so, what other methodologies to implement asynchrony tasks in single thread in java/kotlin?” Can anybody give me an example in android specific ? (or in java/kotlin is welcomed too)
Advertisement
Answer
I think what you are looking for is the concept of async/await
, found in languages like C#
and JavaScript
.
Java
doesn’t support this by default, nor are their any popular/widely used libraries that using it. You can simulate it using Futures
like here Java Equivalent of C# async/await?
but its kind sketch.
Kotlin
has the suspend
function modifier that simulates this functionality. Here is a break down https://www.raywenderlich.com/books/kotlin-coroutines-by-tutorials/v2.0/chapters/5-async-await