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.
- 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) { }
}