2014-09-13 24 views
0

我收到以下錯誤:

09-13 06:27:02.268: E/AndroidRuntime(476): at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1961) 
09-13 06:27:02.268: E/AndroidRuntime(476): at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:794) 
09-13 06:27:02.268: E/AndroidRuntime(476): at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1315) 
09-13 06:27:02.268: E/AndroidRuntime(476): at android.os.AsyncTask.execute(AsyncTask.java:394) 

而下面是我的代碼:

private class DownloadVerses extends AsyncTask<String, Void, String> { 

     @Override 
     protected String doInBackground(String... params) { 

      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
      String resultString = ""; 
      try { 
       boolean resultBoolean = Utils.downloadTurboVerseFile(params[0]); 

       if(resultBoolean){ 
        int progressPercentage = Integer.parseInt(params[2].substring(0,params[2].indexOf("."))); 
        resultString = "Downloading: "+params[1]; 

       } 
       else{ 

        resultString = "ERROR Downloading: "+params[1]; 
        this.doInBackground(params); 
       } 
      } catch (Exception e) { 
       Thread.interrupted(); 
       String exceptionString = e.toString(); 
      } 

      return resultString; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      if(result.contains("ERROR")){ 
       downloading.setTextColor(Color.parseColor("#f05036")); 

      } 
      else{ 
       downloading.setTextColor(Color.parseColor("#79a1ad")); 

      } 

      downloading.setText(result); 

      if(checkIfVersesDownloaded()){ 

       downloadProgressBar.setVisibility(View.INVISIBLE); 
       downloading.setText("Verses Have Been Downloaded."); 
       homeButton.setEnabled(true); 
       homeButton.setVisibility(View.VISIBLE); 

      } 



     } 

     @Override 
     protected void onPreExecute() {} 

     @Override 
     protected void onProgressUpdate(Void... values) {} 
    } 

我執行這樣的代碼:

while(i < verseTitles.size()){  
    new DownloadVerses().execute(verseMp3s.get(i), verseTitles.get(i),progressString); 
    i++;          
} 

我在想這可能是一個簡單的解決方案,需要向我的DownloadVerses私人課程添加一些代碼?

+0

'verseTitles.size()'有多大? – CommonsWare 2014-09-13 12:49:45

+0

verseTitles是139,但它可能高達250.但我們只是說139。 – james0200101 2014-09-13 13:52:54

回答

0

您不能在這樣的循環中創建大量的AsyncTask s。在你看到錯誤之前,你可以創建稍多於128個。

的選項有:

  1. 不要爲每首詩創建單獨的任務。例如,有一個任務可以處理所有的經文。

  2. 提供您自己的定製ThreadPoolExecutor,該定製具有更大的待處理任務隊列,並通過AsyncTaskexecuteOnExecutor()配合使用。

  3. 根本不要使用AsyncTask,而是使用其他後臺線程解決方案。

相關問題