title: Race condition in the Service(Android) (Too many threads)
body: <p>I don't know why, but this function is going in the loop under Timer, user specifies how long he wants to wait in between the updates. Somehow the function halts (hangs) on the part, where "System.out.println("checking to add a new comment");". The interesting thing is that this code runs few times before the whole application freezes, so it doesn't have any errors</p>
  
  <pre><code>public ArrayList&lt;String&gt; newComments = new ArrayList&lt;String&gt;();
  private ArrayList&lt;String&gt; downloadedComments = new ArrayList&lt;String&gt;();
  </code></pre>
  
  <hr>
  
  <pre><code> onStart(){
      if (receivedComments != null) {fthread
                  for (int i = 0; i &lt; receivedComments.size(); i++) {
                      if (newComments.contains(receivedComments.get(i))) {
                          System.out.println("Contains.");
                          newComments.remove(receivedComments.get(i));
                      }
                  }
              }
      }
  </code></pre>
  
  <hr>
  
  <pre><code>private Handler handler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
  
          System.out.println("got msg");
          switch (msg.what) {
          case 1:
              int add = 0;
              System.out.println("start filling the comments");
              for (int i = 1; i &lt; comments.size(); i++) {
                  newComment = comments.get(i).text();
                  // System.out.println(newComment);
                  System.out.println("checking to add a new comment");
                  if (!downloadedComments.contains(newComment)) {
                      newComments.add(newComment);
                      System.out.println("additing");
                      downloadedComments.add(newComment);
                      System.out.println("added");
                      add++;
                      // System.out.println("New comments");
                      // System.out.println(newComments);
                  }
                  // downloadedComments.add(newComment);
              }
  
              // System.out.println(add);
  
              break;
  
          case 2:
              System.out.println("time refresh");
              timeUpdate.scheduleAtFixedRate(new UpdateGui(context,
                      appWidgetManager), 1, 3000);
          }
      }
  };
  </code></pre>
  
  <hr>
  
  <p>UpdateGUI class</p>
  
  <pre><code>private class UpdateGui extends TimerTask {
          Context context;
  
          public UpdateGui(Context context, AppWidgetManager appWidgetManager) {
              this.context = context;
          }
  
          @Override
          public void run() {
              // TODO Auto-generated method stub
              while (true) {
                  for (int i = 0; i &lt; newComments.size(); i++) {
                      try {
                          System.out.println("sleeping");
                          Thread.sleep(3000);
                      } catch (InterruptedException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                      if (newComments.size() &gt; 0) {
                          notificationView.setViewVisibility(
                                  R.id.widget_notificationtext,
                                  LinearLayout.VISIBLE);
                          notificationView.setTextViewText(
                                  R.id.widget_notificationtext,
                                  String.valueOf(newComments.size()));
                      } else {
                          notificationView.setViewVisibility(
                                  R.id.widget_notificationtext,
                                  LinearLayout.INVISIBLE);
                      }
                      if (newComments.size() != 0) {
                          remoteViews.setTextViewText(R.id.widget_text,
                                  newComments.get(i));
                      } else {
                          remoteViews.setTextViewText(R.id.widget_text,
                                  "No unread comments");
                      }
                      appWidgetManager.updateAppWidget(thisWidget, remoteViews);
                      appWidgetManager.updateAppWidget(thisWidget,
                              notificationView);
                  }
  try {
                          System.out.println("sleeping");
                          Thread.sleep(3000);
                      } catch (InterruptedException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
              }
          }
  
      }
  </code></pre>
  
  <p>Since I am waiting every time 3 seconds to update my GUI from the thread, i did not consider what to do when there is nothing to update it with, so the code is being executed at once, but now i added a three seconds delay there as well and everything works just fine. I know that this is not the best design of the application, but i could not come up with anything more eficient than this for now.
  This was a typical race condition during the update when there is nothing to update, so a simple sleep would help</p>