LoginSignup
8
8

More than 5 years have passed since last update.

Android 3.0以降AsyncTaskがsingleThreadな件

Last updated at Posted at 2013-11-26

AsyncTask使って非同期で処理してるはずなのになぜか遅い...
どうも裏の処理を待ってるっぽいので調べたら

Starting HONEYCOMB, tasks are back to being executed on a single thread to avoid common application errors caused by parallel execution.
Developers:AsyncTask.execute

Σ(・ω・ノ)ノ
という訳でAndroid 3.0以降は同期処理らしい…orz

If you truly want parallel execution, you can use the executeOnExecutor(Executor, Params...) version of this method with THREAD_POOL_EXECUTOR; however, see commentary there for warnings on its use.
Developers:AsyncTask.execute

とのことなのでこんな感じで修正。

AsyncTask.java
AsyncTask asyncTask = new AsyncTask();
- asyncTask.execute(request);
+ if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
+   asyncTask.execute(request);
+ } else {
+   asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, request);
+ }
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8