Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet init method having synchronized block

Tags:

java

servlets

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.

like image 785
sorabh solanki Avatar asked May 08 '13 09:05

sorabh solanki


People also ask

What is a synchronized block in Java?

In Java, a Synchronized block helps in performing synchronization on any particular resource of the function or method. If there are 100 lines of code (LOC) and synchronization has to be done for only 10 lines, then a synchronized block can be used. Synchronized can be used as keyword, method and blocks.

How to synchronize a method with another method in Java?

Synchronized block can be used to perform synchronization on any specific resource of the method. Suppose you have 50 lines of code in your method, but you want to synchronize only 5 lines, you can use synchronized block. If you put all the codes of the method in the synchronized block, it will work same as the synchronized method.

How to synchronize only 5 lines of code in Java?

Suppose we have 50 lines of code in our method, but we want to synchronize only 5 lines, in such cases, we can use synchronized block. If we put all the codes of the method in the synchronized block, it will work same as the synchronized method. Synchronized block is used to lock an object for any shared resource.

What are the limitations of synchronized method in Java?

Concurrency Limitations: Java synchronization does not allow concurrent reads. Decreases Efficiency: Java synchronized method run very slowly and can degrade the performance, so you should synchronize the method when it is absolutely necessary otherwise not and to synchronize block only for critical section of the code.


1 Answers

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.

like image 59
AllTooSir Avatar answered Sep 25 '22 05:09

AllTooSir