javax.servlet.ServletContext set / getAttribute是否安全?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javax.servlet.ServletContext set / getAttribute是否安全?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3150字,纯文字阅读大概需要5分钟。
内容图文
必须在javax.servlet.ServletContext中使用setAttribute()和getAttribute(String),我找不到有关并发访问的预期行为的任何信息.但是,这些操作很可能被不同的线程调用.
A servlet can bind an object attribute into the context by name. Any
attribute bound into a context is available to any other servlet
that is part of the same Web application.
但是,没有关于这些操作的并发行为的信息.查看Apache Tomcat的source code,可以看出它是作为ConcurrentHashMap实现的,从而使其有效地保证了线程安全.
我的问题是,我是否应该始终将这些操作视为不是线程安全的,并让应用程序处理同步,或者是否有一些我缺少的信息?
解决方法:
你可以放心地假设你可以调用getAttribute和setAttribute而不需要同步任何东西,但是你应该让你存储的对象成为线程安全的(最简单的方法是存储不可变的东西).注释中链接的问题是关于在servletContext中存储可变对象,在这种情况下,使用它的线程需要首先获取其锁(the accepted answer解释).
没有拼写要求. Java Concurrency in Practice,第4.5.1节“解释模糊文档”中介绍了这一点:
You are going to have to guess. One way to improve the quality of your guess is to interpret the specification from the perspective of someone who will implement it (such as a container or database vendor), as opposed to someone who will merely use it. Servlets are always called from a container-managed thread, and it is safe to assume that if there is more than one such thread, the container knows this. The servlet container makes available certain objects that provide service to multiple servlets, such as HttpSession or ServletContext. So the servlet container should expect to have these objects accessed concurrently, since it has created multiple threads and called methods like Servlet.service from them that could reasonably be expected to access the ServletContext.
Since it is impossible to imagine a single-threaded context in which these objects would be useful, one has to assume that they have been made thread-safe, even though the specification does not explicitly require this. Besides, if they required client-side locking, on what lock should the client code synchronize? The documentation doesn’t say, and it seems absurd to guess. This “reasonable assumption” is further bolstered by the examples in the specification and official tutorials that show how to access ServletContext or HttpSession and do not use any client-side synchronization.
On the other hand, the objects placed in the ServletContext or HttpSession with setAttribute are owned by the web application, not the servlet container. The servlet specification does not suggest any mechanism for coordinating concurrent access to shared attributes. So attributes stored by the container on behalf of the web application should be thread-safe or effectively immutable. If all the container did was store these attributes on behalf of the web application, another option would be to ensure that they are consistently guarded by a lock when accessed from servlet application code. But because the container may want to serialize objects in the HttpSession for replication or passivation purposes, and the servlet container can’t possibly know your locking protocol, you should make them thread-safe.
内容总结
以上是互联网集市为您收集整理的javax.servlet.ServletContext set / getAttribute是否安全?全部内容,希望文章能够帮你解决javax.servlet.ServletContext set / getAttribute是否安全?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。