android - AsyncTask 需要很长时间才能进入 doInBackground()

标签 android multithreading android-asynctask

我有一个从 SharedPreferences 读取的 AsyncTask。当我安装后第一次启动应用程序时,AsyncTask 需要很长时间才能调用 doInBackground() 方法。

代码如下:

public class ReadSharedPrefsTask extends AsyncTask{

    private static final String TAG = ReadSharedPrefsTask.class.getSimpleName();
    Context context;
    String key;
    Object result;
    OnReadFinishedListener listener;
    long startTime;

    /**
     * @param context the context
     * @param key the shared preferences key
     * @param listener a listener which gets informed when the read operation is finished
     */
    public ReadSharedPrefsTask(Context context, String key, OnReadFinishedListener listener){
        Log.d(TAG, "Ctor begin");
        startTime = System.currentTimeMillis();
        this.context = context.getApplicationContext();
        this.key = key;
        this.listener = listener;
        Log.d(TAG, "Ctor end; elapsed time: " + (System.currentTimeMillis() - startTime));

    }

    @Override
    protected Object doInBackground(Object[] params) {

        Log.d(TAG, "doInBackground; elapsed time: " + (System.currentTimeMillis() - startTime));

        SharedPreferences prefs = MASharedPreferences.get(context);

        Log.d(TAG, "doInBackground; elapsed time: " + (System.currentTimeMillis() - startTime));

        result = prefs.getAll().get(key);

        Log.d(TAG, "doInBackground; elapsed time: " + (System.currentTimeMillis() - startTime));

        return result;
    }

    @Override
    protected void onPostExecute(Object o) {

        Log.d(TAG, "onPostExecute; elapsed time: " + (System.currentTimeMillis() - startTime));
        super.onPostExecute(o);
        listener.onReadFinished(o, key);
    }

}

我是这样称呼任务的:

new ReadSharedPrefsTask(getContext(), PREF_COUPON_IDS, this).execute();

然后 logcat 打印:

D/ReadSharedPrefsTask﹕ Ctor begin
D/ReadSharedPrefsTask﹕ Ctor end; elapsed time: 3
D/ReadSharedPrefsTask﹕ doInBackground; elapsed time: 126478
D/ReadSharedPrefsTask﹕ doInBackground; elapsed time: 126480
D/ReadSharedPrefsTask﹕ doInBackground; elapsed time: 126481
D/ReadSharedPrefsTask﹕ onPostExecute; elapsed time: 126481

如您所见,从完成构造函数到调用 doInBackground 之间的时间超过两分钟!

我在 Samsumng Galaxy S3 (4.2.2) 和 Nexus 4 (5.1) 上对其进行了测试,这在两个设备上都会发生。但它不会发生在 genymotion 模拟器上。

有人知道这种行为吗?可能是什么原因导致的?

最佳答案

在此之前您是否执行过任何其他异步任务,启动的 android API 11 AsyncTask 默认按顺序执行。

要运行彼此独立的任务,您必须使用 AsyncTask.THREAD_POOL_EXECUTOR。这需要不同的执行者:

   // Go parallel!
   task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

关于android - AsyncTask 需要很长时间才能进入 doInBackground(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30618600/

相关文章:

c - 线程问题

c - 使用互斥锁时, "printf"输出两次(或更多?我不确定)?

android - AsyncTask onPostExecute - 新的 Intent 错误

java - 在 ASyncTask.execute() 之后从 Activity 执行 x()

android - 异步任务中的 toast

java - 通过 JNI (Android NDK) 将对象 (DisplayMetrics) 从 C 传递到 Java

Android: ping 没有/system/bin/ping

c++ - 如何卸载高度依赖于正确异常处理的计算繁重的任务?

android - 替换 Tablayout 中的 fragment

java - sqlite + listview + 新 Activity : close() was never explicitly called on database?