5

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.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
sorabh solanki
  • 442
  • 1
  • 7
  • 18
  • This might help you: http://stackoverflow.com/questions/7826452/servelt-thread-pool-vs-servlet-instance-pool-by-the-web-container – Uooo May 08 '13 at 09:16

3 Answers3

4

init() will be executed only once when the Servlet is initially loaded into the container on its single instance which the Container will create. Then doGet() and doPost() methods will be executed for each request as separate threads of execution.I don't see any point of synchronizing init() method or any code inside it. Even in a distributed environment there may be one Servlet instance per JVM.I think the Container is wise enough to call init() only once in the Servlet's lifetime and hence there will not be any contention between multiple threads to execute init(). As per Javadocs,

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.

NINCOMPOOP
  • 46,742
  • 15
  • 123
  • 158
1

As the comment says

// This is a safeguard against running init() more than once.

This is just trying to save some kind of race-condition, which should never happen.

Any Servlet, Filter and Listener lives as long as the webapp lives. They are being shared among all requests in all sessions.

So you can say that init() method gets called once, in the container. So there is no need to have a synchronization block.

But, you are taking the lock at Class-level not at object level, any reasons for the same? You should be checking for any synchronized static calls, there can be a valid reason for having this block. If not you can remove the same.

You can see some good explanation here

Community
  • 1
  • 1
Himanshu Bhardwaj
  • 3,798
  • 2
  • 14
  • 35
0

In general I concur with your assessment.

It doesn't appear to be implementing singlethreading so removing the "sychronized" shouldn't hurt.

On the other hand it causes almost no loss of perfomance so removal is solely for "cruft reduction".

Jaydee
  • 4,090
  • 1
  • 16
  • 20