doInBackground需要很长时间才能开始

时间:2013-11-04 22:29:10

标签: java android multithreading android-asynctask

有谁知道为什么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 ...
   }

}

如果慢速运行的原因是其他干扰线程(我怀疑),我该如何给予这个优先级最高?

1 个答案:

答案 0 :(得分:4)

它取决于您拥有如何执行AsyncTasks的Android版本。它们在API5(2.0) - API 10(2.3.3)上执行。从API 11开始,它们默认按顺序执行。正如zapl所提到的,你必须致电

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

让他们再次兼并。