AsyncTask: doInBackground not called

后端 未结 3 1027
旧巷少年郎
旧巷少年郎 2021-02-03 10:59

I have a problem with the AsyncTask. Sometimes the doInBackground() method is not called after onPreExecute().

I know this questio

3条回答
  •  忘掉有多难
    2021-02-03 11:17

    Finaly I found a solution to the problem.

    Instead of execute the asyncTask with the *AsyncTask.THREAD_POOL_EXECUTOR*, I instanciate my own ThreadPoolExecutor with a large corePoolSize and maximumPoolSize:

    int corePoolSize = 60;
    int maximumPoolSize = 80;
    int keepAliveTime = 10;
    
    BlockingQueue workQueue = new LinkedBlockingQueue(maximumPoolSize);
    Executor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue);
    

    And ...

    asyncTask.executeOnExecutor(threadPoolExecutor);
    

    I don't know if this is a good bugfix but with this, doInBackground() is always called. As you said, I supose that the problem was that *AsyncTask.THREAD_POOL_EXECUTOR* could not manage as much asyncTasks as I gave to it.

    Thank you guys

提交回复
热议问题