I have one question. In my application, i have a servlet whose init code is as below.
public class GameInitServlet extends HttpServlet {
private static boolean initialized = false;
@Override
public void init() throws ServletException {
// This is a safeguard against running init() more than once.
synchronized (GameInitServlet.class) {
if (initialized) {
LOG.error("GameInitServlet has already been initialized... Bailing out!");
return;
}
initialized = true;
}
//some code here....
}
}
NOTE: In web.xml the above servlet is having load-on-startup as 1, so it will get initialized at the time of startup the app.
so my question is why we are synchronizing the init method. After all it will be taken care by servlet container and only get called once. Can i remove the above synchronization process or there will be some impact on the application after removing this.