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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
AsyncTaskPool.java
Description: Binary data

