File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
laoying Yin
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Up!
I puzzle too.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. While it should have no noticeable effect, it may be highlighting a misunderstanding from the original author.
 
Muhammad Safwat
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is completely unnecessary. It will not affect security, and its only effect on performance would be negative (if any).
 
laoying Yin
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic