AsyncTask ; doInbackground() not called after calling method onPreExecute()

前端 未结 4 1212
星月不相逢
星月不相逢 2021-02-01 03:34

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         


        
相关标签:
4条回答
  • 2021-02-01 04:17

    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);
    
    0 讨论(0)
  • 2021-02-01 04:27

    I was in the same situation.

    1. I made a call AsyncTask.cancel(boolean mayInterruptIfRunning) passing true
    2. Then I assumed (I was wrong) that the doInBackground() of this task was now interrupted

    By 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...

    0 讨论(0)
  • 2021-02-01 04:31

    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();
        }
    }
    
    0 讨论(0)
  • 2021-02-01 04:36

    Doing this not working for you?

    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.

    0 讨论(0)
提交回复
热议问题