Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
Greenhorn
Posts: 20
  • 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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Up!
I puzzle too.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 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.
 
Greenhorn
Posts: 12
  • 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
  • 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
  • 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?
 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
  • 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?
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic