In my android App, I use multiple AsyncTask using THREAD_POOL_EXECUTOR which makes the tasks run in parallel. Sometime the app hangs. Below is the code I use.
在我的android应用程序中,我使用THREAD_POOL_EXECUTOR使用多个AsyncTask,使任务并行运行。有时应用程序挂起。下面是我使用的代码。
- Could you please let me know how to fine tune so as to avoid any hanging issue ?
- 你能告诉我如何调好调子,以免有任何悬而未决的问题吗?
-
How to find the point in which app is hanging ?
如何找到应用的挂点?
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) { }
}
}
线程的细节
2 个解决方案
#1
1
Instead of using Async Tasks, i recommend using RXJAVA for this purpose.
我建议使用RXJAVA来实现这个目的,而不是使用异步任务。
Here is the disadvantage given for Async Task: https://*.com/a/9654445/9100553
异步任务的缺点是:https://*.com/a/9654445/9100553
Using RxJava will solve this problem, here is an perfect blog which can solve your problem of Multi Threading using RxJava.
使用RxJava将解决这个问题,这里有一个完美的博客,可以使用RxJava解决多线程问题。
http://www.nurkiewicz.com/2017/09/idiomatic-concurrency-flatmap-vs.html
http://www.nurkiewicz.com/2017/09/idiomatic-concurrency-flatmap-vs.html
(Read the second half) Both flatmap and parallel operators would be useful in your case.
(读下半部分)平面映射和并行运算符在您的例子中都是有用的。
#2
1
Check the AsyncTaskLoader
concept. This feature is supported by Android community introduced in API level 11 along with Honeycomb features.
检查AsyncTaskLoader概念。该特性由API级11中引入的Android社区支持,并带有蜂巢特性。
AsyncTaskLoader
solved the lot of limitations & workaround solutions of the AsyncTask.java
AsyncTaskLoader解决了AsyncTask.java的许多限制和解决方案
Official : https://developer.android.com/reference/android/content/AsyncTaskLoader.html
官方:https://developer.android.com/reference/android/content/AsyncTaskLoader.html
Good Sample: https://medium.com/google-developers/making-loading-data-on-android-lifecycle-aware-897e12760832
好示例:https://medium.com/google developers/making -加载数据——- android -生命周期-了解- 897 e12760832
public class JsonAsyncTaskLoader extends AsyncTaskLoader<List<String>> {
// You probably have something more complicated
// than just a String. Roll with me
private List<String> mData;
public JsonAsyncTaskLoader(Context context) {
super(context);
}
@Override
protected void onStartLoading() {
if (mData != null) {
// Use cached data
deliverResult(mData);
} else {
// We have no data, so kick off loading it
forceLoad();
}
}
@Override
public List<String> loadInBackground() {
// This is on a background thread
// Good to know: the Context returned by getContext()
// is the application context
File jsonFile = new File(
getContext().getFilesDir(), "downloaded.json");
List<String> data = new ArrayList<>();
// Parse the JSON using the library of your choice
// Check isLoadInBackgroundCanceled() to cancel out early
return data;
}
@Override
public void deliverResult(List<String> data) {
// We’ll save the data for later retrieval
mData = data;
// We can do any pre-processing we want here
// Just remember this is on the UI thread so nothing lengthy!
super.deliverResult(data);
}
}
#1
1
Instead of using Async Tasks, i recommend using RXJAVA for this purpose.
我建议使用RXJAVA来实现这个目的,而不是使用异步任务。
Here is the disadvantage given for Async Task: https://*.com/a/9654445/9100553
异步任务的缺点是:https://*.com/a/9654445/9100553
Using RxJava will solve this problem, here is an perfect blog which can solve your problem of Multi Threading using RxJava.
使用RxJava将解决这个问题,这里有一个完美的博客,可以使用RxJava解决多线程问题。
http://www.nurkiewicz.com/2017/09/idiomatic-concurrency-flatmap-vs.html
http://www.nurkiewicz.com/2017/09/idiomatic-concurrency-flatmap-vs.html
(Read the second half) Both flatmap and parallel operators would be useful in your case.
(读下半部分)平面映射和并行运算符在您的例子中都是有用的。
#2
1
Check the AsyncTaskLoader
concept. This feature is supported by Android community introduced in API level 11 along with Honeycomb features.
检查AsyncTaskLoader概念。该特性由API级11中引入的Android社区支持,并带有蜂巢特性。
AsyncTaskLoader
solved the lot of limitations & workaround solutions of the AsyncTask.java
AsyncTaskLoader解决了AsyncTask.java的许多限制和解决方案
Official : https://developer.android.com/reference/android/content/AsyncTaskLoader.html
官方:https://developer.android.com/reference/android/content/AsyncTaskLoader.html
Good Sample: https://medium.com/google-developers/making-loading-data-on-android-lifecycle-aware-897e12760832
好示例:https://medium.com/google developers/making -加载数据——- android -生命周期-了解- 897 e12760832
public class JsonAsyncTaskLoader extends AsyncTaskLoader<List<String>> {
// You probably have something more complicated
// than just a String. Roll with me
private List<String> mData;
public JsonAsyncTaskLoader(Context context) {
super(context);
}
@Override
protected void onStartLoading() {
if (mData != null) {
// Use cached data
deliverResult(mData);
} else {
// We have no data, so kick off loading it
forceLoad();
}
}
@Override
public List<String> loadInBackground() {
// This is on a background thread
// Good to know: the Context returned by getContext()
// is the application context
File jsonFile = new File(
getContext().getFilesDir(), "downloaded.json");
List<String> data = new ArrayList<>();
// Parse the JSON using the library of your choice
// Check isLoadInBackgroundCanceled() to cancel out early
return data;
}
@Override
public void deliverResult(List<String> data) {
// We’ll save the data for later retrieval
mData = data;
// We can do any pre-processing we want here
// Just remember this is on the UI thread so nothing lengthy!
super.deliverResult(data);
}
}