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
Asking which one is “best” is the wrong approach here – it depends on what you are trying to accomplish.
Threads).post()aRunnabledirectly, orsendMessage()aMessage(along with other options, such as providing a delay before processing aRunnableorMessage). However,Handlerisn’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 otherThreadto do a process in the background, and inside of it wouldpostaRunnableusing theHandlerwhen 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 couldpostaRunnablewith a delay to activate later, and then at the endpostitself again with a delay.Params,Progress, andResultgeneric types are used to provide start parameters, progress update data, and end result data, respectively. Internally,AsyncTaskusesThreads,Runnables, andHandlers to accomplish this task.