有谁知道为什么doInBackground需要很长时间才能启动?这是一个例子:

runDoInBackground(){
   print("starting task");
   new MyAsyncTask(someObject,otherObject).execute((Void)null);
}

private class MyAsyncTask extends<Void,Void,Integer>{
   SomeObject someObject;
   OtherObject otherObject;

   public MyAsyncTask(SomeObject someObject, OtherObject otherObject){
      this.someObject=someObject;
      this.otherObject = otherObject;
   }

   @Override
   protected Integer doInBackground(Void... params) {
      print("start background run")
      ... work ... get from server ...
   }

}

如果运行缓慢的原因是其他干扰线程(我对此表示怀疑),我该如何赋予这一最高优先级?

最佳答案

它确实取决于您如何执行AsyncTasks的Android版本。它们在API5(2.0)-API 10(2.3.3)上并行执行。从API 11开始,默认情况下会再次顺序执行它们。正如zapl所说,您必须致电

task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void)null);

让他们再次平行运行。

07-26 03:42