• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.base.task;
6 
7 import java.util.ArrayDeque;
8 import java.util.concurrent.Executor;
9 
10 class SerialExecutor implements Executor {
11     final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>();
12     Runnable mActive;
13 
14     @Override
execute(final Runnable r)15     public synchronized void execute(final Runnable r) {
16         mTasks.offer(new Runnable() {
17             @Override
18             public void run() {
19                 try {
20                     r.run();
21                 } finally {
22                     scheduleNext();
23                 }
24             }
25         });
26         if (mActive == null) {
27             scheduleNext();
28         }
29     }
30 
scheduleNext()31     protected synchronized void scheduleNext() {
32         if ((mActive = mTasks.poll()) != null) {
33             AsyncTask.THREAD_POOL_EXECUTOR.execute(mActive);
34         }
35     }
36 }
37