[英]How does my ThreadLocal gets reset every request even though we have a thread pool
I noticed something interesting. 我注意到一些有趣的事
I was told (and from what I read) that it is safe to hold request-scope variables in a ThreadLocal
(let's say you don't have access to the request object and can't use request attributes) 我被告知(并且从我读到的内容)在ThreadLocal
保存请求范围变量是安全的(假设您没有访问请求对象并且不能使用请求属性)
Well, it seems to work (at least when I checked in tomcat). 好吧,它似乎工作(至少当我检查tomcat时)。 eg Even if I have 10 threads in the pool, the thread local variables seem to live only for the scope of a single request. 即使我在池中有10个线程,线程局部变量似乎只适用于单个请求的范围。 I mean, even if I see the same thread name (let's say I have 10 in the pool, so after 10 requests I'm ought to see some repeat) each request "magically" resets all thread local variables for that thread. 我的意思是,即使我看到相同的线程名称(假设我在池中有10个,所以在10个请求之后我应该看到一些重复)每个请求“神奇地”重置该线程的所有线程局部变量。
Is that true? 真的吗?
Is every time a request thread is returned to the pool, it clears all thread local vars? 每次将请求线程返回到池中时,它是否会清除所有线程本地变量? How? 怎么样?
Later tomcat versions have a thread local protection mechanism to avoid memory leaks and especially defend against hanging class loaders on application re-deploy. 后来的tomcat版本有一个线程本地保护机制,以避免内存泄漏,特别是在应用程序重新部署时防止挂起的类加载器。
TC 6.0.24 detects it and 7.0.6 removes the thread locals, as documented here: http://wiki.apache.org/tomcat/MemoryLeakProtection TC 6.0.24检测到它,7.0.6删除线程本地,如下所示: http ://wiki.apache.org/tomcat/MemoryLeakProtection
So this is not normal for threads/threadpools, but a TC feature. 所以这对于线程/线程池来说并不正常,而是TC功能。
Is every time a request thread is returned to the pool, it clears all thread local vars? 每次将请求线程返回到池中时,它是否会清除所有线程本地变量?
It seems that Tomcat took care of house-cleaning but in general the answer is No, and this was one of the reason that was causing Out Of Memory errors in Glassfish 3.0.1 似乎Tomcat负责清洁房屋,但总的来说答案是否定的,这也是导致Glassfish 3.0.1内存出现内存错误的原因之一
In Glassfish 3.0.1, for instance during application deployment some code was creating a ThreadLocal variable holding a reference to some instances which in turn seems to hold a reference to a lot of other objects (mostly related to proxy classes generated during EJB and CDI deployment). 在Glassfish 3.0.1中,例如在应用程序部署期间,一些代码创建了一个ThreadLocal变量,该变量包含对某些实例的引用,而这些实例似乎还包含对许多其他对象的引用(主要与EJB和CDI部署期间生成的代理类相关) )。 Glassfish doesn't seem to clean this reference after the deployment finishes, which wouldn't be much of a problem if the thread that deploys the application terminates. 部署完成后,Glassfish似乎不会清除此引用,如果部署应用程序的线程终止,这将不会有太大问题。
Unfortunately the application deployment thread never dies, because it is not created solely for the purpose of application deployment. 遗憾的是,应用程序部署线程永远不会死亡,因为它不是仅为应用程序部署而创建的。 It is instead taken from the Glassfish thread pool and expected behaviour is returned to the pool once it finishes the deployment task. 它取自Glassfish线程池,一旦完成部署任务,预期行为就会返回到池中。 That means the ThreadLocal reference never gets cleaned and over time causes the heap to overflow. 这意味着ThreadLocal引用永远不会被清除,并且随着时间的推移会导致堆溢出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.