I did added async library at my project and checked already, I don\'t know why code flow doesn\'t go in to asynctask
Code
public void doMysql()
{
Log
Quoting Android Documentation for Async Task
When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.
If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.
Use this snippet for triggering AsyncTask:
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
asyncTaskInstance.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
else
pdfPageChanger.execute(params);
I was in the same situation.
AsyncTask.cancel(boolean mayInterruptIfRunning)
passing truedoInBackground()
of this task was now interruptedBy adding logs statements I realised that the very first asyncTask
was still running ... thus preventing another asyncTask
to execute. It's doInBackground()
method was running an infinite while loop so I simply had to change condition to consider all the possible "break" cases...
You should execute your task with executor
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Because in lower versions of Android all AsyncTasks were executed at single background thread. So new tasks might be waiting, until other task working.
In lower versions in Android (actually on pre-HONEYCOMB) you can't execute AsyncTask on executor.
Change your code to
public void executeAsyncTask()
{
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.e("AsyncTask", "onPreExecute");
}
@Override
protected String doInBackground(Void... params) {
Log.v("AsyncTask", "doInBackground");
String msg = null;
// some calculation logic of msg variable
return msg;
}
@Override
protected void onPostExecute(String msg) {
Log.v("AsyncTask", "onPostExecute");
}
};
if(Build.VERSION.SDK_INT >= 11/*HONEYCOMB*/) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
task.execute();
}
}
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
I was already doing this and the problem was still there, the problem was I was using AsyncTask
to run long background tasks that do run forever, so the ThreadPoolExecutor
's worker threads were already occupied.
In my case AsyncTask.THREAD_POOL_EXECUTOR
(use debugger to get your executor properties) was having the ff properties:
maximumPoolSize = 3
largetsPoolSize = 2
corePoolSize =2
So disabling one of the long running asyncTasks made my doInBackground code to run but that is not the right solution.
The right solution is I moved long running tasks into custom threads rather than using AsyncTask
.