title: How to execute one thread after the other in a sequential manner to store data in Room
body: <p>I am currently trying to add a few records into my local room DB in a sequential manner.</p>
  <p>But It seems every time I execute a runnable on a single thread using Executor. It still returns data in random order.</p>
  <p>Method Call</p>
  <pre><code>   for (int i = 0; i &lt; mImagePaths.size(); i++) {
                  strDate = sdf.format(c.getTime());
  
  
                  Sequential_Fix sequential_fix = new Sequential_Fix(lastInsertid, new MyImages(&quot;&quot;+i, strDate, lastInsertid, mImagePaths.get(i), scantype, 0, 0, &quot;N&quot;, &quot;N&quot;, sync3, &quot;Y&quot;, &quot;Y&quot;, &quot;Y&quot;), i);
                  new DatabaseHelper(getApplicationContext()).getImageCount(sequential_fix, lastInsertid, new Callback&lt;Long&gt;() {
                      @Override
                      public void result(Long ong1) {
                          insertedIdsList.add(ong1);
                      }
  
                  });
  
  
              }
  </code></pre>
  <p>My Method</p>
  <pre><code>    Executor executor = Executors.newSingleThreadExecutor();
  
   public void getImageCount(Sequential_Fix data,int lastInsertid, Callback&lt;Long&gt; longCallback) {
          executor.execute(new Runnable() {
              @Override
              public void run() {
                  AppDatabase db = AppDatabase.getDatabase(mContext.getApplicationContext());
                  data.getImage().setName(String.valueOf(db.myImagesDao().getLastPosOfFolder(lastInsertid)));
                  Log.e(DatabaseHelper.class.getSimpleName(), &quot;run: &quot;+data.getPos() );
                  longCallback.result(db.myImagesDao().insertImage(data.getImage()));
              }
          });
      }
  </code></pre>
  <p>LogCat</p>
  <pre><code>2022-09-03 15:32:47.035 15396-17206/scancan.com.myscanner E/DatabaseHelper: run: 0
  2022-09-03 15:32:47.041 15396-17210/scancan.com.myscanner E/DatabaseHelper: run: 3
  2022-09-03 15:32:47.042 15396-17207/scancan.com.myscanner E/DatabaseHelper: run: 1
  2022-09-03 15:32:47.042 15396-17209/scancan.com.myscanner E/DatabaseHelper: run: 2
  2022-09-03 15:32:47.043 15396-17212/scancan.com.myscanner E/DatabaseHelper: run: 4
  </code></pre>