Issue
Can you explain short guide about background thread in 2021? I mean what is last recommendation to use, what was deprecated.
When I tried to found the information about it, I have confused. One place told about Thread
, another Executor
, the next about AsyncTask
and so on. Every contains note or comment about obsolescence and not recommended by Google
. What is true?
Solution
It really depends on what you are trying to do. Generally, the guide to background processing which was already linked by Dmitry in your comments is a good place to start.
Regarding the things you mentioned:
A Thread
is useful if you have long-running tasks which do not happen all the time. Then you can create a thread whenever needed and it will be destroyed as soon as it's done executing.
By Executor
I guess you are referring to the use of an ExecutorService
. This is a class to manage a pool of threads. That is, you can give a job to the ExecutorService and it will assign it to any of its threads for execution. This is more efficient than creating a thread yourself every time, because the threads are not immediately destroyed after their work is complete. However, they will also stay in the memory because of that. Thus, an ExecutorService is suitable if you have short background jobs which occur often.
As for the AsyncTask
, this API is indeed deprecated by now.
Answered By - Jolly
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.