public final class Scope extends Object
Scope 各个方法读写数据supplyWithExistScope(com.github.phantomthief.scope.Scope, com.github.phantomthief.util.ThrowableSupplier<T, X>) 或者 runWithExistScope(com.github.phantomthief.scope.Scope, com.github.phantomthief.util.ThrowableRunnable<X>) 绑定已经存在的scope
ScopeKey<String> TEST_KEY = allocate();
runWithNewScope(() -> {
TEST_KEY.set("abc");
String result = TEST_KEY.get(); // get "abc"
Scope scope = getCurrentScope();
executor.execute(wrapRunnableExistScope(scope, () -> {
String resultInScope = TEST_KEY.get(); // get "abc"
});
});
TODO: 当前实现是一种比较简易的方式,直接把所有Scope放到一个ThreadLocal里。
实际上这样在使用过程中会有二次hash查询的问题,对性能会有些许的影响,更好的做法是:
直接使用ThreadLocal(也就是使用内部的ThreadLocalMap),同时在Scope拷贝和清理时,维护一个额外的Set,进行ThreadLocal拷贝。
这样的优化考虑是:Scope正常访问的频率很高,而线程切换拷贝的概率比较低。
目前这个实现参考了 GRPC 的 Context API 以及 Spring 的 RequestContext,
相对比较简单,目前效率也可以接受。等到需要榨取性能时再对这个实现动手吧。| Constructor and Description |
|---|
Scope() |
| Modifier and Type | Method and Description |
|---|---|
static Scope |
beginScope()
正常应该优先使用
supplyWithNewScope(com.github.phantomthief.util.ThrowableSupplier<T, X>) 或者 runWithNewScope(com.github.phantomthief.util.ThrowableRunnable<X>)
手工使用beginScope和endScope的场景只有在:
上面两个方法当需要抛出多个正交异常时会造成不必要的try/catch代码
开始scope和结束scope不在一个代码块中
|
static void |
endScope() |
static boolean |
fastThreadLocalEnabled() |
<T> T |
get(ScopeKey<T> key) |
static Scope |
getCurrentScope() |
static <X extends Throwable> |
runWithExistScope(Scope scope,
com.github.phantomthief.util.ThrowableRunnable<X> runnable) |
static <X extends Throwable> |
runWithNewScope(com.github.phantomthief.util.ThrowableRunnable<X> runnable) |
<T> void |
set(ScopeKey<T> key,
T value) |
static <T,X extends Throwable> |
supplyWithExistScope(Scope scope,
com.github.phantomthief.util.ThrowableSupplier<T,X> supplier) |
static <T,X extends Throwable> |
supplyWithNewScope(com.github.phantomthief.util.ThrowableSupplier<T,X> supplier) |
static boolean |
tryEnableFastThreadLocal() |
@Beta public static boolean fastThreadLocalEnabled()
@Beta public static boolean tryEnableFastThreadLocal()
true if fast thread local was enabled.public static <X extends Throwable> void runWithExistScope(@Nullable Scope scope, com.github.phantomthief.util.ThrowableRunnable<X> runnable) throws X extends Throwable
X extends Throwablepublic static <T,X extends Throwable> T supplyWithExistScope(@Nullable Scope scope, com.github.phantomthief.util.ThrowableSupplier<T,X> supplier) throws X extends Throwable
X extends Throwablepublic static <X extends Throwable> void runWithNewScope(@Nonnull com.github.phantomthief.util.ThrowableRunnable<X> runnable) throws X extends Throwable
X extends Throwablepublic static <T,X extends Throwable> T supplyWithNewScope(@Nonnull com.github.phantomthief.util.ThrowableSupplier<T,X> supplier) throws X extends Throwable
X extends Throwable@Nonnull public static Scope beginScope()
supplyWithNewScope(com.github.phantomthief.util.ThrowableSupplier<T, X>) 或者 runWithNewScope(com.github.phantomthief.util.ThrowableRunnable<X>)
手工使用beginScope和endScope的场景只有在:
IllegalStateException - if try to start a new scope in an exist scope.public static void endScope()
beginScope()Copyright © 2019. All rights reserved.