InheritableThreadLocal和线程池 [英] InheritableThreadLocal and thread pools

查看:681
本文介绍了InheritableThreadLocal和线程池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我真的不认为有解决方案,但无论如何我都会尝试。我的应用程序使用线程池,并且此池中的某些线程具有可继承的线程局部变量。我已经扩展了ThreadPoolExecutor类,以便在线程完成执行时基本清除线程局部变量(在afterExecute回调方法中)。

I have a problem which I don't really think has a solution but I'll try here anyway. My application uses a thread pool and some of the threads in this pool have an inheritable thread local variable. I've extended the ThreadPoolExecutor class to essentially clear out the thread local variable (in the afterExecute call back method) when a thread is done executing.

我知道当你有一个InheritableThreadLocal变量时,会在初始化线程时调用childValue()方法,以从父线程获取ThreadLocal变量的值。但是,在我的情况下,下次使用该线程时(在使用一次之后),InheritableThreadLocal变量的值为null(因为它之前已在afterExecute中清除)。有没有办法在beforeExecute中访问父线程的线程局部变量,这样我就可以基本上模拟在创建线程时InheritableThreadLocal中的childValue方法。

I understand that when you have an InheritableThreadLocal variable, the childValue() method is called when the thread is initialized to get the ThreadLocal variable's value from the parent thread. However, in my case the next time the thread is used (after being used once), the value of the InheritableThreadLocal variable is null (because it was previously cleared out in afterExecute). Is there a way to access the parent thread's thread local variable in beforeExecute so that I can essentially simulate what the childValue method in InheritableThreadLocal does at the time of thread creation.

推荐答案

听起来这对线程本地人的可继承风格来说是一个糟糕的用例。

It sounds like this is a poor use-case for the "inheritable" flavour of thread-locals.

我的建议是只需使用常规 TheadLocal 并明确进行初始化;例如将初始值从父线程传递给子线程作为参数或其他东西。

My advice would be to just use a regular TheadLocal and do the initialization explicitly; e.g. passing the initial value from the parent thread to the child thread as a parameter or something.

(我建议你强制初始化线程 - 本地子线程通过让它在启动后立即获取值。但这会冒一个竞争条件;例如,如果父线程在子线程开始执行之前返回到池中。)

(I was going suggest that you force initialization of the thread-local the child thread by having it fetch the value as soon as it starts. But that risks a race-condition; e.g. if the parent thread is returned to the pool before the child thread starts executing.)


我想我要问的是,是否有办法从子线程访问父线程的线程局部变量的值。 / p>

I guess what I am asking is if there is a way to access the value of the parent thread's thread local variable from a child thread.

无法做到这一点。

并且,判断从你的其他评论中,我怀疑你的意思是父母和孩子在正常意义上...父线程创建子线程。

And, judging from your other comments, I doubt that you mean "parent" and "child" in the normal sense ... where the parent thread creates the child thread.

但这里是一个主意。不是尝试在线程之间共享变量,而是共享固定值(例如请求ID),并将其用作共享 Map 的键。使用 Map 条目作为共享变量。

But here's an idea. Instead of trying to share a variable between threads, share a fixed value (e.g. a request ID), and use that as a key for a shared Map. Use the Map entries as the shared variables.

这篇关于InheritableThreadLocal和线程池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆