Real difference between AsyncTask and Thread

前端 未结 6 1176
有刺的猬
有刺的猬 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: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

提交回复
热议问题