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

- #126

Closed
xxd137494375 opened this issue Jul 18, 2023 · 3 comments
Closed

- #126

xxd137494375 opened this issue Jul 18, 2023 · 3 comments
Labels

Comments

@xxd137494375
Copy link

xxd137494375 commented Jul 18, 2023

在看2.14.2版本的源码中,发现registerThreadLocal函数在处理threadLocalHolder并发修改问题时,即加了锁,又使用了copy on write思想。对于这里有个疑问,加了锁,已经保证了线程安全,为何还要使用copy on write? 是在考虑什么隐藏问题吗?

public static boolean registerThreadLocal(@nonnull ThreadLocal threadLocal, @nonnull TtlCopier copier, boolean force) {
if (threadLocal instanceof TransmittableThreadLocal) {
logger.warning("register a TransmittableThreadLocal instance, this is unnecessary!");
return true;
}

        synchronized (threadLocalHolderUpdateLock) {
            if (!force && threadLocalHolder.containsKey(threadLocal)) return false;

            WeakHashMap<ThreadLocal<Object>, TtlCopier<Object>> newHolder = new WeakHashMap<>(threadLocalHolder);
            newHolder.put((ThreadLocal<Object>) threadLocal, (TtlCopier<Object>) copier);
            threadLocalHolder = newHolder;
            return true;
        }
    }
@oldratlee
Copy link
Owner

oldratlee commented Jul 18, 2023

@xxd137494375 好问题~ 👍 TTL的 issue 请重新提到: 💕
https://github.com/alibaba/transmittable-thread-local/issues

PS: 问一下,为什么你会提这个库里来?(或说怎么导航到这个库来的)

@xxd137494375
Copy link
Author

https://github.com/sanbit/transmittable-thread-local/blob/master/README-EN.md 从这个链接里点进来的。。。

@oldratlee
Copy link
Owner

oldratlee commented Jul 18, 2023

https://github.com/sanbit/transmittable-thread-local/blob/master/README-EN.md 从这个链接里点进来的。。。

好的。

我再确认了一下:目前TTL官方库中没有了;之前有链接错误的,已修正。


https://github.com/alibaba/transmittable-thread-local/issues
再提一下吧~


this issue is moved to alibaba/transmittable-thread-local#528

@oldratlee oldratlee changed the title registerThreadLocal方法加锁和copy on write疑问 - Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants