title: Java - Android : Thread being called (run) twice
body: <p>I would like some help regarding Java - Android MultiThreading</p>
  
  <p>While learning to develop my app in a multi-threading way in order to take advantage of the ever-growing multi-core devices market share (most devices are quad core now, some even octo-core), I ran in a situation where my threads are either being calling twice or running twice.</p>
  
  <p>I just don't why and how.</p>
  
  <p><strong>[EDIT 3]</strong>
  Alright, I narrowed down the issue : I called the <code>AsyncTask</code> from the <code>onResume()</code> method. Although my app did not lost focus (which would mean a call to <code>onPause()</code> then back to <code>onResume()</code> upon return of focus in which case my threads would be run twice) during the tests, I solved the issue by moving away the call to <code>FetchFriendsList</code> to another place.
  So far so good, but since in my tests the app did not loose focus or perhaps it did but I could not witness it (!), I think there is another reason behind so I'd say my problem is not entirely solved ... at least for the moment. It does work though. Perhaps I did solve the issue but I do not know how :(
  <strong>[end of EDIT 3]</strong></p>
  
  <p>I am implementing last Facebook SDK and I am using it to fetch the end-user friends list, which seems to do the work.
  Since I am running this operation in an AsyncTask, I am not using <code>request.executeAsync()</code>.
  Instead I am using <code>request.executeAndWait()</code>. Facebook JavaDoc does state that this method must only be used if I am not in a the Main UI Thread which is my case otherwise I would get a <code>NetworkOnMainThreadException</code>.</p>
  
  <p>Anyway, this is where the weird behavior is happening.</p>
  
  <pre><code>private final ArrayList&lt;GraphUser&gt; userFriendsList = new ArrayList&lt;GraphUser&gt;(); 
  public final void fetchFriendsList() {
      if (this.session != null &amp;&amp; this.session.isOpened()) {
          final Request requestUserFriendsList = Request.newMyFriendsRequest(
                  this.session, new Request.GraphUserListCallback()                                         
                      public final void onCompleted(final List&lt;GraphUser&gt; users, final Response response) {
                          if (users != null &amp;&amp; users.size() &gt; 0) {
                              Log.v("Retrieved Friends List -&gt; ", String.valueOf(users.size()));
                              userFriendsList.addAll(users);
                          }
                      }
                  }
          );
          if (this.asyncFlag)
              requestUserFriendsList.executeAsync();
          else
              requestUserFriendsList.executeAndWait();
      }
  }
  </code></pre>
  
  <p>In my case, <code>asyncFlag</code> is set to false because I need to do stuff synchronously in that specific order :</p>
  
  <ol>
  <li>Fetch User Friends List (not on the Main (UI) Thread)</li>
  <li>Save friends list on device (separate new thread)</li>
  <li>Save friends list on a server (separate new thread)</li>
  </ol>
  
  <p>Following this pattern, the line <code>userFriendsList.addAll(users);</code> is called <strong>twice</strong>.
  In the logcat, the <code>Log.v</code>is showed twice as well, and finally looking with the debugger, the content of the user friends list is made of duplicates.</p>
  
  <p>But that's not all ...  step 2 and 3 are indeed two separate threads which are both created and spawned within the same method : <code>public final void asyncSaveFacebookFriendsList()</code>.</p>
  
  <p>And guess what, this method is even called <strong>twice</strong> !</p>
  
  <p><strong>just why ?</strong></p>
  
  <p>At the beginning I was calling the method for step 2 and 3 like this :</p>
  
  <pre><code>[...]
  userFriendsList.addAll(users);
  asyncSaveFacebookFriendsList(); // it was private before
  [...]
  </code></pre>
  
  <p>This is where the issue started as both line were running twice.</p>
  
  <p>So I thought, alright, I'll call it later like this :</p>
  
  <pre><code>[...]
  fetchFriendsList();
  asyncSaveFacebookFriendsList(); // it is now public
  [...]
  </code></pre>
  
  <p>But the issue remains still.
  If I don't call <code>public final void asyncSaveFacebookFriendsList()</code>, then nothing is run <strong>twice</strong>.</p>
  
  <p>Why does this issue happen ? Is there something I did not get in Java Threads ?</p>
  
  <p>I do not think this is somehow related to the Facebook SDK because following the same pattern (and doing it also at the same time), I have the same issues when fetching and storing the end-user Twitter friends list.</p>
  
  <p>So I do believe I am doing something wrong. Does someone have any idea in what possible case a thread is called twice ?</p>
  
  <p><strong>Note</strong> : all threads are started this way : <code>thread.start()</code>. I am not using any <code>ThreadPool</code> nor the <code>ExecutorService</code>.</p>
  
  <p>In case you need more background context :
  Content of AsyncTask : (no need to wonder why Void and Long, I remove the irrelevant code related to it)</p>
  
  <pre><code>private final class FetchFriendsLists extends AsyncTask&lt;Long, Integer, Void&gt; {
          protected final Void doInBackground(final Long... params) {
              if (params[0] != Long.valueOf(-1)) {
                  [...]
                  twitterAPI.fetchUserFriendsList();
                  publishProgress(1, -1);
              }
              if (params[1] == Long.valueOf(0)) {
                  [...]
                  facebookAPI.fetchFriendsList();
                  publishProgress(-1, 0);
              }
              return null;
          }
  
          protected final void onProgressUpdate(Integer... flags) {
              super.onProgressUpdate(flags);
              if (flags[0] != -1)
                  twitterAPI.asyncSaveFacebookFriendsList();
              if (flags[1] == 0)
                  facebookAPI.asyncSaveFacebookFriendsList();
          }
      }
  </code></pre>
  
  <p>As you can see, I start step 2 and 3 in <code>onPublishProgress()</code> which runs on the Main UI Thread. Brefore it was in the <code>doInBackground()</code> method : the issue happens in both cases!</p>
  
  <p>[EDIT]
  After further test, it would seem any kind of code is in fact running twice.
  I created a simple method called test in which in print a counter. The counter incremente twice as well !</p>