Difference between AsyncTask and Thread/Runnable

AndroidMultithreadingAndroid AsynctaskRunnableAndroid Threading

Android Problem Overview


I have question which puzzles me.

Imagine I wanna do something in another thread, like fetching GPS/Location stuff, which as recommended in the SDK documents, must use a background thread.

So here is the question: What's the difference between

  1. Creating a Thread in background via AsyncTask AND

  2. Creating Thread thread1 = new Thread(new Runnable() ... and implementing run()?

Android Solutions


Solution 1 - Android

AsyncTask is a convenience class for doing some work on a new thread and use the results on the thread from which it got called (usually the UI thread) when finished. It's just a wrapper which uses a couple of runnables but handles all the intricacies of creating the thread and handling messaging between the threads.

>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. > >AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask. > >An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.

The Runnable interface is at the core of Java threading. The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread.

Also if I quote from this blog:

>if you need SIMPLE coding use AsyncTask and if you need SPEED use traditional java Thread.

Solution 2 - Android

Also take in count that starting on Android v4.04, you can't have more than one AsyncTasks at a time, unless you lose compatibility with lower versions. Be aware!

Solution 3 - Android

Key differences:

  1. AsyncTask is an asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. It can't be done with normal thread unless you use Handler on UI Thread and post a message OR directly change attribute of an object by implementing proper synchronization.

  2. As recommended by developer guide regarding Thread performance, > There are a few important performance aspects to keep in mind. First, by default, an app pushes all of the AsyncTask objects it creates into a single thread. Therefore, they execute in serial fashion, and—as with the main thread—an especially long work packet can block the queue. For this reason, we suggest that you only use AsyncTask to handle work items shorter than 5ms in duration..

But normal Thread can be used for long running tasks.

Plain java Threads are not much useful for Android unlike HandlerThread, which has been provided by Android framework.

> Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes. Note that start() must still be called.

Refer to below post to know more details:

https://stackoverflow.com/questions/6964011/handler-vs-asynctask-vs-thread/45908614#45908614

Solution 4 - Android

AsyncTask deprecated with api level 30. Using thread/runnable is convenient

Solution 5 - Android

One obvious drawback for AsyncTask class is that after Android 3.0 asynctasks are executed according to the sequence of the start time. that is tasks are executed one by one, unless you execute the task by calling 'executeOnExecutor(Executors.newCachedThreadPool())'. this method would create your own thread pool.

Solution 6 - Android

Better to use the new Job Scheduler in the support library.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkevoroidView Question on Stackoverflow
Solution 1 - AndroidAllTooSirView Answer on Stackoverflow
Solution 2 - AndroidLorenzoView Answer on Stackoverflow
Solution 3 - AndroidRavindra babuView Answer on Stackoverflow
Solution 4 - AndroidE.OguzView Answer on Stackoverflow
Solution 5 - AndroidYuanView Answer on Stackoverflow
Solution 6 - Androiduser10754009View Answer on Stackoverflow