• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.util.concurrent;
18 
19 import com.google.common.base.Preconditions;
20 import com.google.common.collect.Lists;
21 
22 import java.util.List;
23 import java.util.concurrent.Executor;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 
27 /**
28  * <p>A list of ({@code Runnable}, {@code Executor}) pairs that guarantees
29  * that every {@code Runnable} that is added using the add method will be
30  * executed in its associated {@code Executor} after {@link #run()} is called.
31  * {@code Runnable}s added after {@code run} is called are still guaranteed to
32  * execute.
33  *
34  * @author Nishant Thakkar
35  * @author Sven Mawson
36  * @since 2009.09.15 <b>tentative</b>
37  */
38 public class ExecutionList implements Runnable {
39 
40   // Logger to log exceptions caught when running runnables.
41   private static final Logger LOG =
42       Logger.getLogger(ExecutionList.class.getName());
43 
44   // The list of runnable,executor pairs to execute.  Only modified within
45   // a synchronized block.
46   private final List<RunnableExecutorPair> runnables = Lists.newArrayList();
47 
48   // Boolean we use mark when execution has started.  Only accessed from within
49   // synchronized blocks.
50   private boolean executed = false;
51 
52   /**
53    * Add the runnable/executor pair to the list of pairs to execute.  Executes
54    * the pair immediately if we've already started execution.
55    */
add(Runnable runnable, Executor executor)56   public void add(Runnable runnable, Executor executor) {
57 
58     // Fail fast on a null.  We throw NPE here because the contract of
59     // Executor states that it throws NPE on null listener, so we propagate
60     // that contract up into the add method as well.
61     Preconditions.checkNotNull(runnable, "Runnable was null.");
62     Preconditions.checkNotNull(executor, "Executor was null.");
63 
64     boolean executeImmediate = false;
65 
66     // Lock while we check state.  We must maintain the lock while adding the
67     // new pair so that another thread can't run the list out from under us.
68     // We only add to the list if we have not yet started execution.
69     synchronized (runnables) {
70       if (!executed) {
71         runnables.add(new RunnableExecutorPair(runnable, executor));
72       } else {
73         executeImmediate = true;
74       }
75     }
76 
77     // Execute the runnable immediately.  Because of scheduling this may end up
78     // getting called before some of the previously added runnables, but we're
79     // ok with that.  If we want to change the contract to guarantee ordering
80     // among runnables we'd have to modify the logic here to allow it.
81     if (executeImmediate) {
82       executor.execute(runnable);
83     }
84   }
85 
86   /**
87    * Runs this execution list, executing all pairs in the order they were
88    * added.  Pairs added after this method has started executing the list will
89    * be executed immediately.
90    */
run()91   public void run() {
92 
93     // Lock while we update our state so the add method above will finish adding
94     // any listeners before we start to run them.
95     synchronized (runnables) {
96       executed = true;
97     }
98 
99     // At this point the runnable list will never be modified again, so we are
100     // safe running it outside of the synchronized block.
101     for (RunnableExecutorPair runnableAndExecutor : runnables) {
102       runnableAndExecutor.execute();
103     }
104   }
105 
106   private static class RunnableExecutorPair {
107     final Runnable runnable;
108     final Executor executor;
109 
RunnableExecutorPair(Runnable runnable, Executor executor)110     RunnableExecutorPair(Runnable runnable, Executor executor) {
111       this.runnable = runnable;
112       this.executor = executor;
113     }
114 
execute()115     void execute() {
116       try {
117         executor.execute(runnable);
118       } catch (RuntimeException e) {
119         // Log it and keep going, bad runnable and/or executor.  Don't
120         // punish the other runnables if we're given a bad one.  We only
121         // catch RuntimeException because we want Errors to propagate up.
122         LOG.log(Level.SEVERE, "RuntimeException while executing runnable "
123             + runnable + " with executor " + executor, e);
124       }
125     }
126   }
127 }
128