Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ThreadLocal integration #130

Closed
driventokill opened this issue Feb 28, 2019 · 2 comments
Closed

ThreadLocal integration #130

driventokill opened this issue Feb 28, 2019 · 2 comments
Assignees

Comments

@driventokill
Copy link
Member

driventokill commented Feb 28, 2019

In the user code, ThreadLocal can be passed as a cross-thread transmit capability by defining it as TransmittableThreadLocal. However, if the user relies on a library that uses the ThreadLocal implementation, it cannot modify its code at this time, so it cannot be easily used in asynchronous. The ability to get context transmit in the code.

One possible solution is the integration transmit for ThreadLocal.

Users can register the ThreadLocal that needs to be passed, and can provide two methods:

// ThreadLocal will be transmitted after this
<T> boolean TransmittableThreadLocal.register(ThreadLocal<T> threadLocal);

// Then this ThreadLocal will not be transmitted
<T> boolean TransmittableThreadLocal.unregister(ThreadLocal<T> threadLocal);

ThreadLocal集成支持

用户代码中 ThreadLocal 通过定义为 TransmittableThreadLocal 即可获得跨线程传递能力,但是如果用户依赖的库中使用的是 ThreadLocal 实现,这个时候又无法修改其代码,就不能很方便的在异步化的代码中获得上下文传递的能力了。

一个可行的解决方式是开放集成的能力。

用户可以将需要传递的 ThreadLocal 注册进来,可以提供两个方法:

// 接下来这个 ThreadLocal 会被传递
<T> boolean TransmittableThreadLocal.register(ThreadLocal<T> threadLocal);

// 接下来这个 ThreadLocal 不会被传递
<T> boolean TransmittableThreadLocal.unregister(ThreadLocal<T> threadLocal);
driventokill added a commit that referenced this issue Feb 28, 2019
Sometimes we need to use the thread local defined in thirdparty libraries.
Support register ThreadLocal to be transmitted with TransmittableThreadLocals.
@oldratlee oldratlee changed the title ThreadLocal 集成支持 ThreadLocal integration Jul 11, 2019
@oldratlee
Copy link
Member

oldratlee commented Jul 11, 2019

The integration transmit for ThreadLocal cause the potential break of semantics of ThreadLocal:

  • the Thread Confinement from ThreadLocal.
    • aka. the thread-safe relied on the ThreadLocal does no longer established. 💣

Integrate ThreadLocal with a copy lambda can express this risk explicitly, just like the copy method in TransmittableThreadLocal.

📔 Of course, the potential break of semantics of ThreadLocal also exists in java std lib class InheritableThreadLocal, and the method childValue(T) express this risk explicitly.

// ThreadLocal will be transmitted after this
<T> boolean Transmitter.register(
    ThreadLocal<T> threadLocal, Function<T, T> copyLambda);


// Then this ThreadLocal will not be transmitted
<T> boolean Transmitter.unregister(ThreadLocal<T> threadLocal);

cc @driventokill


ThreadLocal的集成传输 会导致 潜在打破ThreadLocal的语义:

  • ThreadLocal保证的线程封闭。
    • 即,依赖于ThreadLocal的线程安全不再成立。 💣

集成ThreadLocal,提供一个用于拷贝的Lambda可以明确地表达出此风险,就像TransmittableThreadLocalcopy方法一样。

📔 当然,Java标准类库的InheritableThreadLocal也一样潜在打破ThreadLocal语义,而方法childValue(T)明确表达了此风险。

// 接下来这个 ThreadLocal 会被传递
<T> boolean Transmitter.register(
    ThreadLocal<T> threadLocal, Function<T, T> copyLambda);

// 接下来这个 ThreadLocal 不会被传递
<T> boolean Transmitter.unregister(ThreadLocal<T> threadLocal);

@oldratlee oldratlee reopened this Jul 11, 2019
@oldratlee oldratlee removed the invalid label Jul 11, 2019
oldratlee pushed a commit that referenced this issue Jul 15, 2019
Sometimes we need to use the thread local defined in thirdparty libraries.
Support register ThreadLocal to be transmitted with TransmittableThreadLocals.
oldratlee pushed a commit that referenced this issue Jul 15, 2019
Sometimes we need to use the thread local defined in thirdparty libraries.
Support register ThreadLocal to be transmitted with TransmittableThreadLocals.
@oldratlee
Copy link
Member

oldratlee commented Aug 10, 2019

released in v2.11.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants