0

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)

foseja
  • 233
  • 2
  • 10
  • `I believe this is general idea in programming world. so what I wonder about this is "is it true in android(java/kotlin)?` you say that this is a general idea for all of programming, then ask if it is true for java or kotlin, do these not form part of the programming world ? – a_local_nobody Jan 21 '22 at 17:22
  • @a_local_nobody if you mean that my thought applies to java or kotlin too, could you give me an example or any link that exmplains it well plz? – foseja Jan 23 '22 at 20:52

1 Answers1

1

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

avalerio
  • 2,072
  • 1
  • 12
  • 11
  • Basically what you are saying is that "there are not any popular/widely used libraries that using async works with single thread in Java/Kotlin" which means most of async works in Android(Java/Kotlin) should be involving multi threads (suspend also involves background threads)? – foseja Jan 24 '22 at 02:04
  • Yes, Kotlin's `suspend` is close to `async/await` functionality because it is uses a "state machine". But unlike `C#` and `Javascript` you can't run it on the main thread without blocking the main thread. AFAIK. IMO `async/await` w/ a single thread is a quick solution to blocking problems, but a moot point for `Android`. In `Android` the main thread is already doing a lot of work as it is, making it do more work won't result in a better experience, it just wouldn't crash due to a not responding error. – avalerio Jan 24 '22 at 02:54