ASyncTasks blocking others

后端 未结 3 1280
梦谈多话
梦谈多话 2021-01-31 16:37

I\'ve 2 ASyncTasks, one retrieves a value from an httpPost and the other update some elements of the UI (including an listview). The problem is that since both ASyncTasks share

3条回答
  •  半阙折子戏
    2021-01-31 17:06

    A slightly more general way to do this is to put two helper methods in a utility class like so:

    class Utils {
    
        @SuppressLint("NewApi")
        public static > void execute(T task, P... params) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
            } else {
                task.execute(params);
            }
        }
    }
    

    Then you can execute tasks with Utils.execute(mytask) or Utils.execute(mytask, params) and it will take care of executing them in parallel.

提交回复
热议问题