标记在“init”中定义的servlet实例变量作为“挥发性” [英] Marking servlet instance variables defined in "init" as "volatile"

查看:204
本文介绍了标记在“init”中定义的servlet实例变量作为“挥发性”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有必要将所有servlet实例变量标记为volatile(或从同步部分访问它们)?包括在init方法中定义的,而不是以后修改的那些?



我理解init方法由一个线程调用,由另一个线程,所以似乎是必要的。或者可能不是?是否有任何机制保证当init方法完成时,实例变量的当前值对所有其他线程可见?

解决方案

servlet容器将确保在servlet的初始化和它们的调用之间存在内存屏障。你不应该做任何事情。



注意:我没有在规格中找到这样的要求,但我会认为这是容器的失败,而不是做它。 BTW,在GenericServlet中,由init方法初始化的实例变量 ServletConfig config 未标记为volatile。


Is it necessary to mark all the servlet instance variables as "volatile" (or to access them from within synchronized sections)? Including those defined in the "init" method, and not modified afterwards?

I understand that the "init" method is called by one thread, and the variable will be accessed by another thread, so it seems to be necessary. Or maybe not? Is there any mechanism that guarantees that the current values of instance variables will be visible to all the other threads when the "init" method is finished?

解决方案

The servlet container will make sure there is a memory barrier between the initialization of the servlets and their invocations. You shouldn't have to do anything.

Note: I haven't found such a requirement in the spec, but I would consider it a failure of the container not to do it. BTW, in GenericServlet, the instance variable ServletConfig config initialized by the init method is not marked as volatile.

这篇关于标记在“init”中定义的servlet实例变量作为“挥发性”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆