Code example for ArrayDeque

Methods: offer, poll

0
   private static class SerialExecutor implements Executor {
      final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>();
      Runnable mActive;
 
      public synchronized void execute(final Runnable r) {
         mTasks.offer(new Runnable() {
            public void run() { 
               try { 
                  r.run();
               } finally { 
                  scheduleNext(); 
               } 
            } 
         }); 
         if (mActive == null) {
            scheduleNext(); 
         } 
      } 
 
      protected synchronized void scheduleNext() { 
         if ((mActive = mTasks.poll()) != null) {
            THREAD_POOL_EXECUTOR.execute(mActive);
         } 
      } 
   }