aspose file tools*
The moose likes Servlets and the fly likes what's the benefit to add Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "what Watch "what New topic
Author

what's the benefit to add "synchronized" before "init" method?

laoying Yin
Greenhorn

Joined: Sep 17, 2002
Posts: 20
Here's the details of the configuration and source code:

web.xml
<servlet>
...
<load-on-startup>0</load-on-startup>
</servlet>

SampleServlet.java

public void synchronized init(ServletConfig config) {
....
calcuate();
}

public void synchronized calcuate() {
....
}

My question: why the author add "synchronized" before "init"? is there any benefit?

In my opnion, the init method should be executed by container only once, so we do not need to worry about synchronizing.
cheng jian
Greenhorn

Joined: Jul 28, 2008
Posts: 7
Up!
I puzzle too.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I agree. While it should have no noticeable effect, it may be highlighting a misunderstanding from the original author.
Muhammad Safwat
Greenhorn

Joined: Aug 12, 2008
Posts: 12
The synchronized keyword is added to ensure that it will only be accessed by just one thread at a time

by default the servlet is not thread safe


http://www.thejavacode.com/
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

From the API for Servlet.init(ServletConfig config)

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
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?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

It is completely unnecessary. It will not affect security, and its only effect on performance would be negative (if any).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
laoying Yin
Greenhorn

Joined: Sep 17, 2002
Posts: 20
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?
 
jQuery in Action, 2nd edition
 
subject: what's the benefit to add "synchronized" before "init" method?