Second AsyncTask not executing

后端 未结 5 450
暗喜
暗喜 2021-01-04 22:34

I have 2 AsyncTask, one which is creating a socket connections and anotherone that is transmitting objects using those sockets. my code is this:

try {
               


        
相关标签:
5条回答
  • 2021-01-04 22:45

    Have you tried executing transmitter in connector's onPostExecute() or vice versa ?

    0 讨论(0)
  • 2021-01-04 22:48

    I hated it when HONEY COMB changed the multiple AsyncTask execution from concurrent to sequential. So every time I execute an AsyncTask, I do something like this.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    } else {
        task.execute();
    }
    

    But the thread pool size is 5, if you add the sixth task, it will be added in a queue, and will not be executed until one of the 5 thread has finished.

    0 讨论(0)
  • 2021-01-04 23:00

    Since I don't know which API level you are using, I would suggest you to go through ASYNC TASK

    According to the documentation:

    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.

    0 讨论(0)
  • 2021-01-04 23:08

    Like Robin Chander said, it's likely running serially and you can use executeOnExecutor...but it isn't available until Android 3.0. If you want full compatibility, you need to dump AsyncTask and use Runnable. If you need to report results on the UI thread, don't forget to use a Handler.

    0 讨论(0)
  • 2021-01-04 23:12

    I had wrote class for execute AsyncTask concurrently.

    Check it out:

    Android-AsyncTask-Executor

    It takes all work for running AsyncTask concurrently on any Android OS version, it is better that using:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    } else {
        task.execute();
    }
    

    Because AsyncTask.THREAD_POOL_EXECUTOR available only on api >= 11

    With my class you just need to write:

    AsyncTaskExecutor.executeConcurrently(task, params);
    

    And that is all. No errors on Android 2.x, 3.x and 4.x

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