Package: ExecutionList$RunnableExecutorPair

ExecutionList$RunnableExecutorPair

nameinstructionbranchcomplexitylinemethod
ExecutionList.RunnableExecutorPair(Runnable, Executor)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
execute()
M: 26 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

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