How AsyncTask works in Android?

by admin

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 package such as Executor, ThreadPoolExecutor and FutureTask.

How many AsyncTask can be started at a time in an Android app?

Ans. The ThreadPoolExecutor has a corePoolSize, a maxPoolSize and a QueueCapacity. If all of your threads from the corePoolSize are used, any other task are put in the queue of each of the threads from the core pool. After these queues are full, a new thread is being initialized, as long as it is within the limits of the maxPoolSize.

CORE_POOL_SIZE = CPU_COUNT + 1;   

CPU_COUNT = Runtime.getRuntime().availableProcessors();

MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;

For more information you refer the source code of Async http://grepcode.com/file/repo1.maven.org/maven2/org.robolectric/android-all/4.4_r1-robolectric-1/android/os/AsyncTask.java

Note: It also depends on android api level.

Leave a Reply

Your email address will not be published.
*
*