简体   繁体   中英

async, background thread, singlethreaded, multithreaded, kotlin coroutines in android

I have read lots of articles on sync, async, multithreading and I think I picked up the main idea/concepts of each terminology. however, there are a couple things that makes me confused.
Before get into my questions, I apologize if any of my statement is incorrect because of misunderstanding. please point wrong statements out and correct them.

  1. when I start an app with basic/empty activity in android studio, this app is single threaded. is it correct?
  2. when I need to add some blocking operations (such as network request, IO operation, and intensive computation), I need to handle blocking operations with async methodologies. is it correct?
  3. there are a couple of async methodologies and they are Threading(dealing with more than 1 thread which is main(UI) thread in android is called multithreading right?), Callbacks, Futures, RxJava, Coroutines etc (if more, list them out please) is it correct?
  4. if I added any type of new thread to do some blocking operations in the app, that new thread would be called background thread and now this app(mentioned in #1 + this new thread as background thread) is called multithreaded. is it correct?
  5. multithreading is a type of async methodologies. is it correct?
  6. async does not have to with multithreading but why do people mention 'background/background thread' when explaning async concept( AsyncTask , Kotlin Coroutines )?
  7. Kotlin Coroutines are about async concept rather than threading(Coroutine can do what Thread does but it is not thread but lightweighted thread), but why is it involved with background thread ? which means an app will be multithreaded whenever I use Coroutines since background thread is involved?

Feel free to rephrase/correct my statements if any looks messy. I appreciate your answer/any links to make my questions/confusions clear. Thank you in advanced.

Since no one really answered my question, I am trying to answer to my question by myself. it is based on my research and there possibly might be some wrong statements. I will appreciate if you can point out anything doesn't look good.

  1. when I start an app with basic/empty activity in android studio, this app is single threaded. is it correct?
    -> Yes , read overview part of this android documentation
  2. when I need to add some blocking operations (such as network request, IO operation, and intensive computation), I need to handle blocking operations with async methodologies. is it correct?
    -> As far as I understood, Yes . Async methodologies should be used to handle blocking operations.
  3. there are a couple of async methodologies and they are Threading(dealing with more than 1 thread which is main(UI) thread in android is called multithreading right?), Callbacks, Futures, RxJava, Coroutines etc (if more, list them out please) is it correct?
    -> Yes . One note on Callbacks. Callbacks could be designed either synchronously or asynchronously. you as a developer need to design Callback asynchronously with threading to handle blocking operations. (Callbacks should be designed either synchronously or asynchronously depending on your matter)
  4. if I added any type of new thread to do some blocking operations in the app, that new thread would be called background thread and now this app(mentioned in #1 + this new thread as background thread) is called multithreaded. is it correct?
    -> Yes , read Worker Thread part of this android documentation
  5. multithreading is a type of async methodologies. is it correct?
    -> Yes
  6. async does not have to with multithreading but why do people mention 'background/background thread' when explaning async concept(AsyncTask, Kotlin Coroutines)?
    -> As far as I understood, async does not have to do with multithreading. However, when it comes to dealing with blocking operations, background thread has to be involved eventually. AsyncTask uses background thread, Kotlin (eventually) uses background thread for blocking operations.
  7. Kotlin Coroutines are about async concept rather than threading(Coroutine can do what Thread does but it is not thread but lightweighted thread), but why is it involved with background thread? which means an app will be multithreaded whenever I use Coroutines since background thread is involved?
    -> I think Yes . App will be multithreaded eventually when using Kotlin Coroutines.

for extra notes on Kotlin Coroutines, if you only use Dispatchers.Main(for UI code/non blocking code) then it might not be multithreaded. However, your code eventually includes some suspend functions for blocking operations since the purpose of using Kotlin Coroutines is to handle blocking operations. any suspend function are called even in Dispatchers.Main, that suspend function(blocking operation) will be switched to background context(Dispatchers.IO, Dispatchers.default) and done by background thread.
This is how I understand on sync, async, threading, Kotlin Coroutines so far.

Please point out and correct my answer if any seems not right or not clear. Also I appreciate your participation on this thread with your thoughts.

Thank you for reading my question and my own answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM