java threadlocal singleton - 它是什么?
通俗地说,当有人说一个对象是 Java 中的线程本地单例时,这意味着什么?我参加了一个关于 Java Server Faces 的讲座,每次谈到 FacesContext 时,讲师总是提醒我们它是一个线程本地单例。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FacesContext
每个线程。FacesServlet
创建一个ThreadLocal
位于 HTTP 的开头servlet 请求并在与 HTTP servlet 请求关联的 HTTP servlet 响应末尾将其删除。每当您执行FacesContext#getCurrentInstance()
在 JSF 代码中,在整个 HTTP servlet 请求/响应处理过程中,您将始终获得同一个实例。由于 HTTP servlet 请求由不同的线程执行,并且 FacesContext 实例作为线程局部变量附加到单个线程,因此没有两个 HTTP servlet 请求共享相同的 FacesContext实例。
There is only one unique instance of the
FacesContext
per thread.The
FacesServlet
creates aThreadLocal<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 aFacesContext#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 sameFacesContext
instance.