【问题标题】:Android AsyncTask onPreExecute() sequential executionAndroid AsyncTask onPreExecute() 顺序执行
【发布时间】:2019-08-25 16:45:58
【问题描述】:

使用 android.os.AsyncTask 时,即使任务通过 executeOnExecutor() 发布到单个线程池,onPreExecute() 方法也会异步执行。这是预期的行为吗?

Google 文档简单说明:

“首次引入时,AsyncTasks 在单个后台线程上串行执行。从 Build.VERSION_CODES.DONUT 开始,这已更改为允许多个任务并行运行的线程池。从 Build.VERSION_CODES.HONEYCOMB 开始,任务在单个线程上执行,以避免并行执行导致的常见应用程序错误。

如果你真的想要并行执行,你可以使用 THREAD_POOL_EXECUTOR 调用 executeOnExecutor(java.util.concurrent.Executor, java.lang.Object[])。"

这并不能说明顺序执行的确切内容。

class testTask extends AsyncTask<String, String, String> {
            @Override
            protected void onPreExecute() {
                //Gets Executed Asynchronously
                Log.v("TAG","onPreExecute");
            }
            @Override
            protected void onPostExecute(String s) {
                // SEEMS to get executed Sequentially as expected
                Log.v("TAG","onPostExecute");
            }
            @Override
            protected String doInBackground(String... strings) {
                // SEEMS to get executed Sequentially as expected
                Log.v("TAG","doInBackground");
                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "";
            }
}
Executor singleThreadExecutor = Executors.newSingleThreadExecutor();
testTask t = new testTask();
t.executeOnExecutor(singleThreadExecutor);
testTask t2 = new testTask();
t2.executeOnExecutor(singleThreadExecutor);

上面的代码输出:

V/TAG: onPreExecute
V/TAG: onPreExecute
V/TAG: doInBackground
V/TAG: onPostExecute
V/TAG: doInBackground
V/TAG: onPostExecute

我期待:

V/TAG: onPreExecute
V/TAG: doInBackground
V/TAG: onPostExecute
V/TAG: onPreExecute
V/TAG: doInBackground
V/TAG: onPostExecute

我希望 AsyncTask 在文档说明时完全按顺序执行。

编辑: 看来这确实是预期的行为。我正在寻找一种方法来完成 AsyncTask 提供的功能

(例如,在 UI 线程上发布一个可运行对象 -> 等待它完成 -> 在单独的线程上执行一个可运行对象 -> 等待它完成 -> 在 UI 线程上发布另一个可运行对象)

只是同步的,所以当两个任务运行时,它们会完全按顺序执行。

【问题讨论】:

  • 你试过像t.execute()这样执行任务吗?

标签: java android


【解决方案1】:

一种可能的解决方案是使用 Java 的线程间通信,如下所示:

class mythread extends Thread {
Runnable pre = new Runnable() {
                    @Override
                    public void run() {
                      //Do Stuff on UI Thread
                      synchronized (this) {
                            this.notify();
                      }
                    }
}
Runnable post = new Runnable() {
                    @Override
                    public void run() {
                      //Do Stuff on UI Thread
                      synchronized (this) {
                            this.notify();
                      }
                    }
}
Runnable doinbackground = new Runnable() {
                    @Override
                    public void run() {
                      //Do Stuff on separate thread
                    }
}
synchronized (pre) {
   runOnUiThread(pre);
   try {
      pre.wait();
   } catch (Exception e) {
      e.printStackTrace();
   }
}
doninbackground.run();
synchronized (post) {
   runOnUiThread(post);
   try {
      post.wait();
   } catch (Exception e) {
      e.printStackTrace();
   }
}
}
Executor singleThreadExecutor = Executors.newSingleThreadExecutor();
singleThreadExecutor.execute(new mythread());
singleThreadExecutor.execute(new mythread());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    相关资源
    最近更新 更多