简体   繁体   English

即使我们有一个线程池,我的ThreadLocal如何重置每个请求

[英]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.

相关问题 Selenium和TestNg并行运行并未将驱动程序附加到我们启动的每个浏览器,即使我使用ThreadLocal概念 - Selenium and TestNg parallel run is not attaching driver to every browser that we launched even though i used ThreadLocal concept 在tomcat的线程池中,当线程返回池时,线程本地值会重置还是删除? - in tomcat's threadpool,When a thread return to pool ,does the threadlocal values reset or remove? 当类暴露给线程池时,清理ThreadLocal资源真的是我的工作吗? - Is it really my job to clean up ThreadLocal resources when classes have been exposed to a thread pool? Spring Boot:我们可以为每个请求设置单独的线程池吗? - Spring Boot: Can we have seperate thread pool for each request? RabbitMQ消费者线程池随着每个请求而增加 - RabbitMQ consumer thread pool increases with every request 如果我的值存储在“线程安全”映射中,那么该值是否需要为ThreadLocal? - If my value is stored in a Thread Safe map does the value need to be ThreadLocal? 即使我们有多个内核可以处理请求,但Singleton Object是否会导致性能下降? - Can Singleton Object lead to degradation in performance even though we have multiple cores to serve request? 即使我正在处理另一个,UI线程也会卡住 - UI Thread gets stuck even though I'm working on another 线程返回线程池后会清除ThreadLocal对象吗? - Will the ThreadLocal object be cleared after thread returned to Thread Pool? 如何引用线程,以及如何在ThreadLocal上进行验证 - How to a reference of a thread, and verification on ThreadLocal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM