/*
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

package com.facebook.imagepipeline.producers;

import com.facebook.common.internal.Preconditions;

import java.util.ArrayList;
import java.util.concurrent.Executor;

public class ThreadHandoffProducerQueue {
  private boolean mQueueing = false;
  private final ArrayList<Runnable> mRunnableList;
  private final Executor mExecutor;

  public ThreadHandoffProducerQueue(Executor executor) {
    mExecutor = Preconditions.checkNotNull(executor);
    mRunnableList = new ArrayList<>();
  }

  public synchronized void addToQueueOrExecute(Runnable runnable) {
    if (mQueueing) {
      mRunnableList.add(runnable);
    } else {
      mExecutor.execute(runnable);
    }
  }

  public synchronized void startQueueing() {
    mQueueing = true;
  }

  public synchronized void stopQueuing() {
    mQueueing = false;
    execInQueue();
  }

  private void execInQueue() {
    for (Runnable runnable : mRunnableList) {
      mExecutor.execute(runnable);
    }
    mRunnableList.clear();
  }

  public void remove(Runnable runnable) {
    mRunnableList.remove(runnable);
  }

  public synchronized boolean isQueueing() {
    return mQueueing;
  }
}

@@ -423,20 +423,45 @@
 il.Array
-List
+Deque;%0Aimport java.util.Deque
 ;%0Aimport
@@ -588,25 +588,21 @@
 e final 
-ArrayList
+Deque
 %3CRunnabl
@@ -796,20 +796,21 @@
 ew Array
-List
+Deque
 %3C%3E();%0A  
@@ -1196,33 +1196,16 @@
     
-for (Runnable runnable : 
+while (!
 mRun
@@ -1213,16 +1213,26 @@
 ableList
+.isEmpty()
 ) %7B%0A    
@@ -1247,32 +1247,43 @@
 tor.execute(
-r
+mR
 unnable
+List.pop()
 );%0A    %7D%0A   
@@ -1303,32 +1303,32 @@
 t.clear();%0A  %7D%0A%0A
-
   public void re
@@ -1320,16 +1320,29 @@
  public 
+synchronized 
 void rem

Changed ArrayList to ArrayDeque so that the runnables are FIFO-ed