java threadlocal singleton-这是什么? [英] java threadlocal singleton - what is it?
问题描述
用外行人的话来说,当有人说一个对象是Java中的线程局部单例时,这意味着什么?我在关于Java Server Faces的讲座中,每次都谈到FacesContext时-讲师总是提醒我们这是一个线程局部单例.
FacesServlet
创建 ThreadLocal<FacesContext>
,并在HTTP servlet结束时将其删除与HTTP Servlet请求关联的HTTP Servlet响应.每当您在以下位置执行 FacesContext#getCurrentInstance()
您的JSF代码,您将在整个HTTP Servlet请求/响应处理过程中始终获得同一实例.
由于HTTP servlet请求是由不同的线程执行的,并且FacesContext
实例作为线程局部变量附加到单个线程,所以没有两个HTTP servlet请求共享相同 FacesContext
实例. /p>
In layman speak, what does it mean when somebody says an object is a threadlocal singleton in Java? I was at a lecture about Java Server Faces, and everytime the FacesContext was spoken of - the instructor always reminded us that it is a threadlocal singleton.
There is only one unique instance of the FacesContext
per thread.
The FacesServlet
creates a ThreadLocal<FacesContext>
on the beginning of the HTTP servlet request and removes it on the end of the HTTP servlet response associated with the HTTP servlet request. Whenever you do a FacesContext#getCurrentInstance()
in your JSF code, you'll always get the one and the same instance throughout the entire HTTP servlet request/response processing.
Since HTTP servlet requests are executed by distinct threads and the FacesContext
instance is attached as thread local variable to a single thread, no two HTTP servlet requests share the same FacesContext
instance.
这篇关于java threadlocal singleton-这是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!