Reputation: 45942
Multithreading on Android
is to some extent an easy task due to the various possibilities available for us.
However it would be nice to understand the difference between the approaches.
What is the best way to multitask and based on what preferences is it the "best"?
AsyncTask
?
class MultiTasker extends AsyncTask<, , >
Runnable
?
Runnable myRun = new Runnable(){
public void run(){
}
};
Thread T = new Thread(myRun);
T.start();
Handler
?
class MultiTasker extends Handler
Upvotes: 2
Views: 123
Reputation: 16898
Asking which one is "best" is the wrong approach here - it depends on what you are trying to accomplish.
Thread
s).post()
a Runnable
directly, or sendMessage()
a Message
(along with other options, such as providing a delay before processing a Runnable
or Message
). However, Handler
isn't something you would use by itself to provide multithreading - it's use is usually to get back into the main activity (UI) thread. You'd start some other Thread
to do a process in the background, and inside of it would post
a Runnable
using the Handler
when you needed to update the UI. Or if you had a task that didn't necessarily need to run in the background, but did need to pop up and do something every so often, you could post
a Runnable
with a delay to activate later, and then at the end post
itself again with a delay.Params
, Progress
, and Result
generic types are used to provide start parameters, progress update data, and end result data, respectively. Internally, AsyncTask
uses Thread
s, Runnable
s, and Handler
s to accomplish this task.Upvotes: 3
Reputation: 13501
Its Always better if you go with AsyncTask()
.. because Thats something which has been built to solve MultiThreading
issues in Android..
Upvotes: 1