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 throw runnable.mExecutedInline; | |
26 } else { | |
27 // It's possible that this method is being called on an executor, an d the runnable that | |
28 // was just queued will run on this thread after the current runnabl e returns. By | |
29 // nulling out the mCallingThread field, the InlineCheckingRunnable' s current thread | |
30 // comparison will not fire. | |
31 runnable.mCallingThread = null; | |
32 } | |
33 } | |
34 | |
35 private static final class InlineCheckingRunnable implements Runnable { | |
36 private final Runnable mCommand; | |
37 private Thread mCallingThread; | |
38 private InlineExecutionProhibitedException mExecutedInline = null; | |
39 | |
40 private InlineCheckingRunnable(Runnable command, Thread callingThread) { | |
41 this.mCommand = command; | |
42 this.mCallingThread = callingThread; | |
43 } | |
44 | |
45 @Override | |
46 public void run() { | |
47 if (Thread.currentThread() == mCallingThread) { | |
48 // Can't throw directly from here, since the delegate executor c ould catch this | |
49 // exception | |
mef
2016/08/31 17:26:52
nit: Comments in Chrome must actually end with per
| |
50 mExecutedInline = new InlineExecutionProhibitedException(); | |
51 return; | |
52 } | |
53 mCommand.run(); | |
54 } | |
55 } | |
56 } | |
OLD | NEW |