Marking servlet instance variables defined in “init” as “volatile”

前端 未结 1 465
感动是毒
感动是毒 2021-01-13 10:23

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\" met

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-13 10:38

    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.

    0 讨论(0)
提交回复
热议问题