title: AsyncTask, must it take such a performance penalty hit...?
body: <p>I'm developing a small app that reads in specific html-pages, re-formats them and then shows them in a WebView. If I run my code in the GUI thread, the performance hit is close to negligible compared to simply letting the WebView show the original html-page. But if I'm a good boy and do like I'm told, I'm supposed to use an AsyncTask to run the code in the background so as not to freeze up the GUI during those 3-5 seconds my code does its job. Problem is... if I do so, the code takes more than 10 times as long to finish. A page takes 60+ seconds to show, which is unacceptable.</p>
  
  <p>Tracking down the problem, TraceView shows me that my AsyncTask is (at default priority) run in roughly 10 ms chunks, around 4 times per second. I need to set my thread priority to MAX_PRIORITY to get close to acceptable loading times, but even then it takes 3-4 times longer than when I run in the GUI thread.</p>
  
  <p>Am I doing something wrong, or is this just the way it works? And must it work this way...?</p>
  
  <p>Here's compilable code as requested:</p>
  
  <pre><code>package my.ownpackage.athome;
  
  import android.app.Activity;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.os.StrictMode;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  
  public class AndroidTestActivity extends Activity
  {   
      WebView webview;
      //...
  
      private class HelloWebViewClient extends WebViewClient 
      {
          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) 
          {
              AndroidTestActivity.this.fetch(view, url);
              return true;
          }
      }
  
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          // To allow to connect to the web and pull down html-files, reset strict mode
          // see http://stackoverflow.com/questions/8706464/defaulthttpclient-to-androidhttpclient
          if (android.os.Build.VERSION.SDK_INT &gt; 9) 
          {
              StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
              StrictMode.setThreadPolicy(policy);
          }
  
          // webview init etc...
  
          fetch(webview, "http://www.example.com");   
      }
  
      // This one calls either the AsyncTask or does it all manually in the GUI thread
      public void fetch(WebView view, String url)
      {
          //** Use these when run as AsyncTask in background - SLOW! 
          //** Takes 30+ seconds at default thread priority, with MAX_PRIORITY 15+ seconds
          // AsyncTask&lt;Void, String, String&gt; fx = new FilterX(url, view, this);   
          // fx.execute();    // running as AsyncTask takes roughly ten times longer than just plain load!    
  
          //** Use these when not running as AsyncTask - FAST! takes ~5 seconds
          FilterX fx = new FilterX(url, view, this);
          fx.onPreExecute();
          final String str = fx.doInBackground();
          fx.onPostExecute(str);
      }
  }
  
  class FilterX extends AsyncTask&lt;Void, String, String&gt;
  {
      WebView the_view = null;
      // other stuff...
  
      FilterX(final String url, final WebView view, final Activity activity)
      {
          the_view = view;
          // other initialization
          // same code in both cases
      }
  
      protected void onPreExecute()
      {
          // same code in both cases
      }
  
      protected String doInBackground(Void... v)
      {
          // same in both cases...
  
          return new String();    // just to make it compile
      }
  
      protected void onPostExecute(final String string)
      {
          the_view.loadUrl(string);
          // same in both cases...
      }
  }
  </code></pre>
  
  <p>To run <strong>exactly</strong> the same code in my FilterX class when run as AsyncTask as when run on the GUI thread, I stripped all ProgressBar stuff, and then I get the following timings:</p>
  
  <ul>
  <li>30+ seconds to load a page at default thread priority </li>
  <li>15+ seconds to load a page at MAX_PRIORITY </li>
  <li>5+ seconds to load a page when run in the GUI thread</li>
  </ul>