I have a servlet that share the access to a Map< String, InnerObject> , with InnerObject being declared as :
public class InnerObject {
volatile EnumObject obj; //declared volatile because can be accessed by different threads of the servlet
//.......(Methods that change the status of the obj
}
and the servlet declared as:
public class TestServlet {
Map<String, InnerObject> map;
public void doGet(..) {
//change the value of the map and the inner objects
}
}
When I try to access the value of the InnerObjects, I get a slow answer from the servlet. If I change the map to "Volatile" too:
volatile Map<String, InnerObject> map;
, I get faster answers from the http request.
Is volatile needed in this case? Why is it slower when the volatile is defined only in the internal object?