title: Suspending all threads took: ms warning using Threads - Android
body: <p>I have 2 <code>Thread</code>s that make some network computation.
  When I run my app and after starting my second <code>Thread</code> I get a:</p>
  
  <p><code>Suspending all threads took: ms</code> 
  warning followed by:</p>
  
  <p><code>Background sticky concurrent mark sweep GC freed 246745(21MB) AllocSpace objects, 169(6MB) LOS objects, 33% free, 31MB/47MB, paused 1.972ms total 127.267ms</code> warning.</p>
  
  <p>Sometimes I get just those 2 warnings and other times I get a lot of those 2 warnings until I decide to terminate the app running. At this point, it's just running the main <code>Thread</code> and basically doing nothing. Here is the relevant code:</p>
  
  <p><strong>MainActivity.java</strong>:</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      // Getting html page through a thread
      this.getHtmlPageThread = new GetHtmlPageThread(URL_STRING);
      this.getHtmlPageThread.start();
  
      // The thread that will search the web for data
      this.getDataFromTheWebThread = new GetDataFromTheWebThread();
  
      // Search button click listener
      searchButton.setOnClickListener(new View.OnClickListener()
      {
          @Override
          public void onClick(View v)
          {
              // Get the searched lyrics
              searchedLyrics = inputEditText.getText().toString();
  
              informUserAboutConnectionToTheNet();
  
              // Starting to search the web for data through a thread
              getDataFromTheWebThread.start();
  
              if (!getDataFromTheWebThread.isAlive())
              {
                  printMap(MainActivity.matchResultMap);
              }
          }
      }); // End of search button click listener
  
      printMap(MainActivity.matchResultMap);
  
  } // End of onCreate() method
  
  protected void onStart()
  {
      super.onStart();
  
      if (!this.isParseSucceeded()) // Connection to net failed
      {
          if (!getHtmlPageThread.isAlive()) // If the thread is not alive, start it.
          {
              getHtmlPageThread.start(); // Try to connect again
              this.informUserAboutConnectionToTheNet();
          }
      }
      if (!this.isParseSucceeded())
      {
          super.onStart(); // Call onStart() method
      }
  
  } // End of onStart() method
  </code></pre>
  
  <p><strong>GetHtmlPageThread.java</strong>:</p>
  
  <pre><code>public class GetHtmlPageThread extends Thread
  {
      private String url;
  
      public GetHtmlPageThread(String url)
      {
          this.url = url;
      }
  
      @Override
      public void run()
      {
          try
          {
              MainActivity.htmlPage.setHtmlDocument(this.getParsedDocument(this.url));
              if (MainActivity.htmlPage.getHtmlDocument() != null)
              {
                  MainActivity.parsedSucceeded = true; // Parsed succeeded
              }
              else
              {
                  MainActivity.parsedSucceeded = false; // Parsed failed
              }
              Thread.sleep(100);
          }
          catch (InterruptedException e)
          {
              e.printStackTrace();
          }
      }
  
      /**
       * Returns the document object of the url parameter.
       * If the connection is failed , return null.
       *
       * @param url Url to parse
       * @return The document of the url.
       *
       */
      public Document getParsedDocument(String url)
      {
          try
          {
              return Jsoup.connect(url).get();
          }
          catch (IOException e) // On error
          {
              e.printStackTrace();
          }
  
          return null; // Failed to connect to the url
      }
  
  }
  </code></pre>
  
  <p><strong>GetDataFromTheWeb.java</strong>:</p>
  
  <pre><code>public class GetDataFromTheWebThread extends Thread
  {
      public static boolean isFinished = false; // False - the thread is still running. True - the thread is dead
  
      @Override
      public void run()
      {
          GetDataFromTheWebThread.isFinished = false;
          try
          {
              this.getLyricsPlanetDotComResults(MainActivity.searchedLyrics); // Method for internet computations
              Thread.sleep(100);
          }
          catch (InterruptedException e)
          {
              e.printStackTrace();
          }
          GetDataFromTheWebThread.isFinished = true;
      }
      ...
  }
  </code></pre>
  
  <p>Basically the <code>this.getLyricsPlanetDotComResults(MainActivity.searchedLyrics);</code> method in the second <code>Thread</code> is doing a lot of the internet work and computations in general. More computations than net stuff to be exact.</p>
  
  <p>So I guess I got those warnings because the second <code>Thread</code> is too "Busy"? Or maybe just my implementation with the activity life cycle with the <code>onCreate()</code> method and <code>onStart()</code> method are wrong? </p>
  
  <p>Needless to say, I don't get the output I want, though I debugged the app and stepped through the second <code>Thread</code> and it works <strong>Perfectly</strong>. So again, it got to be something with my <code>Activity</code>'s implementation.</p>