Real difference between AsyncTask and Thread

前端 未结 6 1173
有刺的猬
有刺的猬 2021-01-01 17:38

I have been reading Android documentation (AsyncTask, Thread) and vogella tutorial about this matter, but I have doubts yet.

For example, I want to send a message fr

相关标签:
6条回答
  • 2021-01-01 18:21

    AsyncTask enables proper and easy use of the UI thread. - from Developer.

    The thing is - AsyncTask is a special kind of Thread - one which is a GUI thread, it works in the background and also let's you do something with the GUI - it is basically "pre-programmed" for you with functions onPreExecute(), do inBackground(), onPostExecute().

    In order to make Thread work that way, you have to write a loooot of code.

    0 讨论(0)
  • 2021-01-01 18:22

    Please read this blog

    http://crazyaboutandroid.blogspot.in/2011/12/difference-between-android.html

    and Details are:

    Difference between Android Service,Thread,IntentService and AsyncTask

    When to use ?

    Service

       Task with no UI, but shouldn't be too long. Use threads within service for long tasks.
    

    Thread

    - Long task in general.
    
    - For tasks in parallel use Multiple threads (traditional mechanisms)
    

    AsyncTask

    - Small task having to communicate with main thread.
    
    - For tasks in parallel use multiple instances OR Executor 
    
    0 讨论(0)
  • 2021-01-01 18:24

    AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

    You can control its own functions

    doInBackground(Params... params), onCancelled(), onPostExecute(Result result), onPreExecute(), nProgressUpdate(Progress... values), publishProgress(Progress... values)

    0 讨论(0)
  • 2021-01-01 18:33

    in general using of 2 this features are equivalent, but AsyncTask is more simple in terms of integration with GUI

    0 讨论(0)
  • 2021-01-01 18:39
    • I would prefer to Use Async Task as it will let you know when the background process gets started and over and when can I parse the response.
    • Async has methods like onPreExecute and onPostExecute which will allow us to do tasks before and after calling the background tasks.
    0 讨论(0)
  • 2021-01-01 18:41

    All other answers here are not complete, there is a big difference between AsyncTask and Thread, i.e.

    Thread can be triggered from any thread, main(UI) or background; but AsyncTask must be triggered from main thread.

    Also on lower API of Android(not sure, maybe API level < 11), one instance of AsyncTask can be executed only once.

    For more information read Difference between Android Service, Thread, IntentService and AsyncTask

    In general

    Thread

    • Long task in general.

    • For tasks in parallel use Multiple threads (traditional mechanisms)

    AsyncTask

    • Small task having to communicate with main thread.

    • For tasks in parallel use multiple instances OR Executor

    0 讨论(0)
提交回复
热议问题