简体   繁体   English

java threadlocal singleton - 它是什么?

[英]java threadlocal singleton - what is it?

In layman speak, what does it mean when somebody says an object is a threadlocal singleton in Java? 外行人说,当有人说某个对象是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. 我正在参加有关Java Server Faces的讲座,每次谈到FacesContext时 - 讲师总是提醒我们这是一个线程单一的单身人士。

There is only one unique instance of the FacesContext per thread. 每个线程只有一个FacesContext唯一实例。

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. FacesServlet在HTTP servlet请求的开头创建一个ThreadLocal<FacesContext> ,并在与HTTP servlet请求关联的HTTP servlet响应的末尾删除它。 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. 每当在JSF代码中执行FacesContext#getCurrentInstance()时,您将始终在整个HTTP Servlet请求/响应处理中获得同一个实例。

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. 由于HTTP servlet请求由不同的线程执行,而FacesContext实例作为线程局部变量附加到单个线程,因此没有两个HTTP servlet请求共享同一个 FacesContext实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM