I use many AsyncTask classes in my app. I always start them using:
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
asyncTask.execute();
}
Recently I got a RejectedExecutionException exception. I read about this and understand why this happens (exceeding the maximum number of tasks that thread pool executor can have in its queue).
But i didn't read anywhere about a way to check if thread pool executor has available slots so that I can avoid this. From what I tested the asyncTask.execute(); method does not have this limitation.
So I'm looking for something like this:
if(executorCanHandlerAnotherQueue()){
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
asyncTask.execute();
}
}
else{
asyncTask.execute();
}
Any ideas on how to implement this with backwards compatibility up to API 9?