悬挂应用程序的多个异步任务

iap*_*ker 11 android android-asynctask threadpoolexecutor

在我的Android应用程序中,我使用THREAD_POOL_EXECUTOR使用多个AsyncTask,这使得任务并行运行.有时应用程序挂起.以下是我使用的代码.

  1. 你能告诉我如何微调以避免任何悬挂问题吗?
  2. 如何找到应用程序挂起的点?

    new fetchInitialCoinsParallel().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url);
    
    prefCoinList = getPrefCoin();
    if(prefCoinList.size()>0){
        for(int i=0;i<prefCoinList.size();i++){
            new fetchAltCoinsParallel().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url);
        }
    }
    
    public class fetchAltCoinsParallel extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
    }
    
    protected String doInBackground(String... params) {
        try {
            InputStream is = getDataFromURL(params[0]);
            if(is!=null){
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                synchronized(this){
                    brList.add(br);
                }
    
            }else{
                prefCoinNotLoadedTimeOutCount=prefCoinNotLoadedTimeOutCount+1;
            }
    
    
            if(brList.size()==prefCoinList.size()-prefCoinNotLoadedTimeOutCount){
                try {
                    loadAltCoins(getAltCoinDataParallel());
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                maingame.dataReady=true;
            }
    
        } catch (IOException e) {
            e.printStackTrace();
        } 
        return null;
    }
    
    protected void onPostExecute(String params) {
    }
    protected void onProgressUpdate(String... progress) {
    }
    
    Run Code Online (Sandbox Code Playgroud)

    }

线程细节 在此输入图像描述

Tak*_*nia 1

检查这个AsyncTaskLoader概念。此功能由 API 级别 11 中引入的 Android 社​​区以及 Honeycomb 功能支持。

\n\n

AsyncTaskLoader解决了 AsyncTask.java 的许多限制和解决方案

\n\n

官方:https://developer.android.com/reference/android/content/AsyncTaskLoader.html

\n\n

好的示例:https://medium.com/google-developers/making-loading-data-on-android-lifecycle-aware-897e12760832

\n\n
public class JsonAsyncTaskLoader extends AsyncTaskLoader<List<String>> {\n    // You probably have something more complicated\n    // than just a String. Roll with me\n    private List<String> mData;\n\n    public JsonAsyncTaskLoader(Context context) {\n        super(context);\n    }\n\n    @Override\n    protected void onStartLoading() {\n        if (mData != null) {\n            // Use cached data\n            deliverResult(mData);\n        } else {\n            // We have no data, so kick off loading it\n            forceLoad();\n        }\n    }\n\n    @Override\n    public List<String> loadInBackground() {\n        // This is on a background thread\n        // Good to know: the Context returned by getContext()\n        // is the application context\n        File jsonFile = new File(\n                getContext().getFilesDir(), "downloaded.json");\n        List<String> data = new ArrayList<>();\n        // Parse the JSON using the library of your choice\n        // Check isLoadInBackgroundCanceled() to cancel out early\n        return data;\n    }\n\n    @Override\n    public void deliverResult(List<String> data) {\n        // We\xe2\x80\x99ll save the data for later retrieval\n        mData = data;\n        // We can do any pre-processing we want here\n        // Just remember this is on the UI thread so nothing lengthy!\n        super.deliverResult(data);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

  • 我根本不会打扰 Android 加载器 - 它们是在 Android 的早期阶段发明的 - 除了带有大量样板和静态类的不可预测的代码之外,它们还提供什么好处 - 自从 rxjava 发明以来已经很久了 - https:// /stackoverflow.com/questions/20762514/why-are-loaders-bad-in-android (2认同)
  • TLDR 我会在审查中通过保留的片段代码,但不会在 2018 年通过加载程序。它们是许多开发人员投入的一个虚构的内容 - 甚至您引用的博客的作者也添加了一条注释,**如果您正在寻找现代的,不依赖加载器的此问题的灵活解决方案(此处选择的解决方案),请查看使用架构组件进行生命周期感知数据加载博客文章。** (2认同)