0

while learning the difference between multi-threading and Concurrency.i follow this stackoverflow answer according to my understanding AsyncTask is just used to on or off the use of main thread{ui thread} while events like http request or fetching data from database. and after task is done main thead is reallocated to the event by AsyncTask task. but Android Official says "An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread"

now i am confused.

  1. android use multi-theading its just wrapper class for thread management.
  2. C# async and await is diffrent concept.
geek
  • 13
  • 2
  • 1
    `AsyncTask` is deprecated . – ADM Mar 14 '21 at 12:16
  • @ADM but i need to know please – geek Mar 14 '21 at 14:24
  • Internally, async task uses single thread for all operations. It keeps incoming tasks in a queue and fetches them one by one so it is not a good practice for network like io operations. C# uses different paradigms that is called coroutines so you don't need to compare coroutines with async tasks. If you want to use it in android programming, you can use Kotlin coroutines. – M.ekici Mar 14 '21 at 17:16
  • @M.ekici if async task internally uses another own single thread then is it multi-threading not Concurrency – geek Mar 15 '21 at 09:23

1 Answers1

0

An AsyncTask "touches" two threads: Main/UI and the Background one. There are few of its methods that run on Main/UI Thread (onPre/onPostExecute()) and one method that is run on a WorkerThread in background. There is an internal sync/queue between those methods.

Concurrency (async/await) doesn't (necessarily) use a second thread but uses "moments of when the CPU is free". Think about this non-real case: if the Main/UI Thread is 100% busy then no any Concurrency could be executed until the Main/UIThread has a bit of "free CPU cycles" to share. Intead, in this last example AsyncTasks will do its job asynchronously without take in count other Threads (and its results will be queued to UI/MainThread to be processed later).

emandt
  • 2,547
  • 2
  • 16
  • 20