Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
The servlet container calls the init method exactly once after instantiating the servlet. The init method must complete successfully before the servlet can receive any requests.
'exactly once'
The init method is part of the Servlet lifecycle and is managed by the container. If it is called exactly once, there should be no need for synchronising the method.
laoying Yin
Greenhorn
Joined: Sep 17, 2002
Posts: 20
posted
0
Originally posted by David O'Meara: From the API for Servlet.init(ServletConfig config)
'exactly once'
The init method is part of the Servlet lifecycle and is managed by the container. If it is called exactly once, there should be no need for synchronising the method.
I think the init method should be execute only once when container initialize the servlet, then all the requests will be processed by service method synchronization or asynchronization. If the container could not guarantee it, so what we could believe?
I just think "synchronized" before init method is redundant, however the author only add it in this servlet and others don't use it, so I think there must be a difference between them ,which I do not know. performance or security?
Originally posted by Bear Bibeault: It is completely unnecessary. It will not affect security, and its only effect on performance would be negative (if any).
Thanks for the conclusion, Bear!
So just like what I thought, no matter with "Synchronized" or not, the init method should be always executed synchronized and just only once, right?
btw, I have another question: If all the methods called by service() method are labeled with "synchronized", which means the real implements of doGet & doPost will process requests synchronizing, is there any difference with implementing SingleThreadModel?
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: what's the benefit to add "synchronized" before "init" method?