Please consider the implications of this: does it even make sense to
have 20+ threads (on a single CPU device) updating the UI at once?
That is NOT the purpose of AsyncTask. Also, since Donut, the limit of
enqueued tasks in AsyncTask is 128, not 20. So AsyncTask has a maximum
of 10 threads running concurrently and can hold up to 128 tasks
waiting for a thread to be freed in the pool. So basically AsyncTask
does exactly what you are trying to do here.

On Wed, Nov 11, 2009 at 1:02 AM, Atif Gulzar <atif.gul...@gmail.com> wrote:
> Android has a limit to rum at MAX 20 concurrent AsyncTask. To handle this
> limit I created a AsyncTaskPool.java utility. Its not a pool in true sense
> but a kind of scheduler. I am posting it here for your comments and it may
> help others.
>
> import java.util.ArrayList;
>
> import android.os.AsyncTask;
>
> public class AsyncTaskPool
> {
>
>     private int poolSize;
>     private ArrayList<AsyncTask> currentTasks = new ArrayList<AsyncTask>();
>     private ArrayList<Object> pendingTasks = new ArrayList<Object>();
>
>     /**
>      * @param poolSize
>      *            : it should be less than 20. As Android only supports max.
> 20 concurrent Asynch tasks.
>      */
>     public AsyncTaskPool(int poolSize)
>     {
>     this.poolSize = poolSize;
>     }
>
>     public int getPoolSize()
>     {
>         return poolSize;
>     }
>
>
>     public boolean addTask(AsyncTask asyncTask, Object... params)
>     {
>
>     if (currentTasks.size() < poolSize)
>     {
>         currentTasks.add(asyncTask);
>         if (params != null)
>         asyncTask.execute(params);
>         else
>         asyncTask.execute();
>     }
>     else
>     {
>         Object[] task = new Object[2];
>         task[0] = asyncTask;
>         task[1] = params;
>
>         pendingTasks.add(task);
>     }
>
>     return true;
>     }
>
>     public boolean removeTask(AsyncTask task)
>     {
>     if(currentTasks.contains(task))
>     {
>         currentTasks.remove(task);
>         return true;
>     }
>     return false;
>     }
>
>     //Add this method in the onPostExecute method of AsyncTask
>     public boolean removeAndExecuteNext(AsyncTask atask)
>     {
>     removeTask(atask);
>     if (pendingTasks.size()>0 && currentTasks.size()<poolSize)
>     {
>         Object [] task = (Object []) pendingTasks.get(0);
>         pendingTasks.remove(task);
>
>         addTask((AsyncTask)task[0], (Object[])task[1]);
>
>     }
>
>     return false;
>     }
>
> }
>
>
>
> --
> Best Regards,
> Atif Gulzar
>
> I ◘◘◘◘ Unicode, ɹɐzlnƃ ɟıʇɐ
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en



-- 
Romain Guy
Android framework engineer
romain...@android.com

Note: please don't send private questions to me, as I don't have time
to provide private support.  All such questions should be posted on
public forums, where I and others can see and answer them

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to