Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (C) 2007 The Guava Authors
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.

A list of listeners, each with an associated Executor, that guarantees that every Runnable that is added will be executed after execute() is called. Any Runnable added after the call to execute is still guaranteed to execute. There is no guarantee, however, that listeners will be executed in the order that they are added.

Exceptions thrown by a listener will be propagated up to the executor. Any exception thrown during Executor.execute (e.g., a RejectedExecutionException or an exception thrown by MoreExecutors.sameThreadExecutor()) will be caught and logged.

Nishant Thakkar
Sven Mawson
 public final class ExecutionList {
   // Logger to log exceptions caught when running runnables.
   private static final Logger log =
   // The runnable,executor pairs to execute.
   private final Queue<RunnableExecutorPairrunnables = Lists.newLinkedList();
   // Boolean we use mark when execution has started.  Only accessed from within
   // synchronized blocks.
   private boolean executed = false;

Creates a new, empty ExecutionList.
   public ExecutionList() {

Adds the Runnable and accompanying Executor to the list of listeners to execute. If execution has already begun, the listener is executed immediately.

Note: For fast, lightweight listeners that would be safe to execute in any thread, consider MoreExecutors.sameThreadExecutor(). For heavier listeners, sameThreadExecutor() carries some caveats: First, the thread that the listener runs in depends on whether the ExecutionList has been executed at the time it is added. In particular, listeners may run in the thread that calls add. Second, the thread that calls execute() may be an internal implementation thread, such as an RPC network thread, and sameThreadExecutor() listeners may run in this thread. Finally, during the execution of a sameThreadExecutor listener, all other registered but unexecuted listeners are prevented from running, even if those listeners are to run in other executors.

   public void add(Runnable runnableExecutor executor) {
     // Fail fast on a null.  We throw NPE here because the contract of
     // Executor states that it throws NPE on null listener, so we propagate
     // that contract up into the add method as well.
     Preconditions.checkNotNull(runnable"Runnable was null.");
     Preconditions.checkNotNull(executor"Executor was null.");
     boolean executeImmediate = false;
     // Lock while we check state.  We must maintain the lock while adding the
     // new pair so that another thread can't run the list out from under us.
     // We only add to the list if we have not yet started execution.
     synchronized () {
       if (!) {
         .add(new RunnableExecutorPair(runnableexecutor));
       } else {
         executeImmediate = true;
    // Execute the runnable immediately. Because of scheduling this may end up
    // getting called before some of the previously added runnables, but we're
    // OK with that.  If we want to change the contract to guarantee ordering
    // among runnables we'd have to modify the logic here to allow it.
    if (executeImmediate) {
      new RunnableExecutorPair(runnableexecutor).execute();

Runs this execution list, executing all existing pairs in the order they were added. However, note that listeners added after this point may be executed before those previously added, and note that the execution order of all listeners is ultimately chosen by the implementations of the supplied executors.

This method is idempotent. Calling it several times in parallel is semantically equivalent to calling it exactly once.

10.0 (present in 1.0 as run)
  public void execute() {
    // Lock while we update our state so the add method above will finish adding
    // any listeners before we start to run them.
    synchronized () {
      if () {
       = true;
    // At this point the runnables will never be modified by another
    // thread, so we are safe using it outside of the synchronized block.
    while (!.isEmpty()) {
  private static class RunnableExecutorPair {
    final Runnable runnable;
    final Executor executor;
    RunnableExecutorPair(Runnable runnableExecutor executor) {
      this. = runnable;
      this. = executor;
    void execute() {
      try {
      } catch (RuntimeException e) {
        // Log it and keep going, bad runnable and/or executor.  Don't
        // punish the other runnables if we're given a bad one.  We only
        // catch RuntimeException because we want Errors to propagate up.
        .log(."RuntimeException while executing runnable "
            +  + " with executor " + e);
New to GrepCode? Check out our FAQ X