OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
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.net; | |
6 | |
7 import java.util.concurrent.Executor; | |
8 | |
9 /** | |
10 * Executor that detects and throws if its mDelegate runs a submitted runnable i nline. | |
11 */ | |
12 final class DirectPreventingExecutor implements Executor { | |
13 private final Executor mDelegate; | |
14 | |
15 DirectPreventingExecutor(Executor delegate) { | |
16 this.mDelegate = delegate; | |
17 } | |
18 | |
19 @Override | |
20 public void execute(Runnable command) { | |
21 Thread currentThread = Thread.currentThread(); | |
22 InlineCheckingRunnable runnable = new InlineCheckingRunnable(command, cu rrentThread); | |
23 mDelegate.execute(runnable); | |
24 if (runnable.mExecutedInline != null) { | |
25 new RuntimeException(runnable.mExecutedInline).printStackTrace(); | |
26 throw runnable.mExecutedInline; | |
27 } else { | |
28 // It's possible that this method is being called on an executor, an d the runnable that | |
29 // was just queued will run on this thread after the current runnabl e returns. By | |
30 // nulling out the mCallingThread field, the InlineCheckingRunnable' s current thread | |
31 // comparison will not fire. | |
32 runnable.mCallingThread = null; | |
33 } | |
34 } | |
35 | |
36 private static final class InlineCheckingRunnable implements Runnable { | |
37 private final Runnable mCommand; | |
38 private Thread mCallingThread; | |
39 private InlineExecutionProhibitedException mExecutedInline = null; | |
40 | |
41 private InlineCheckingRunnable(Runnable command, Thread callingThread) { | |
42 this.mCommand = command; | |
43 this.mCallingThread = callingThread; | |
44 } | |
45 | |
46 @Override | |
47 public void run() { | |
48 if (Thread.currentThread() == mCallingThread) { | |
49 mExecutedInline = new InlineExecutionProhibitedException(); | |
mef
2016/08/30 16:29:44
Can it just throw from here instead of returning?
Charles
2016/08/30 20:11:05
Done.
| |
50 return; | |
51 } | |
52 mCommand.run(); | |
53 } | |
54 } | |
55 } | |
OLD | NEW |