There's no special magic in init(); synchronization works the same there as it does everywhere else. Can you describe in somewhat more detail what you did, what you expected to happen, and what happened instead, being as specific as possible and avoiding the confusing word "it" in favor of using the names of things?
init() will be executed only once when the servlet is initially loaded into the container. Then doGet() and doPost() methods will be executed for each request.
There is no point of synchronizing init() method. You can synchronize code in get() or post() methods Or you can configure servlet container to use SingleThreadModel for synchronization.
gooi chin hin
posted 16 years ago
I have problems with the init function of a servlet because I noticed that the init function actually runs more than one time if there are multiple request to the servlet at the same time when it is loaded.
Try to perform some initialization in the init function but encountered problem as the initialization failed as it is called more than once. Thus I have moved this initialization process to a synchronized function hoping that it will solve the above problem. To my surprised, the system still allow more than one thread to access the synchronized function at one time.
I am not a servlet expert. But i understand that the servlet container can create 'n' number of instance of your servlet.
So for every instance of your servlet that is created your init method will be called.
If you want your init to be called only once - you can go for a single threaded model. That is not good.
Probably you are synchronising on a private method of your servlet class.
Check to see that you are synchronising on a static object.
It is better to provide a simple test sample of what you are doing. Then it can be obvious to people as to what you are doing and what is going wrong.
If i am wrong any where kindly let me know about it.
Thanks Vijay Venkat
gooi chin hin
posted 16 years ago
I checked the servlet log and it shows that the init function is called more than once if there are multiple requests to the servlet at one time. I am trying to call the initLAP in the servlet init() and it will failed if it is called more than once.
OK, well, that's entirely consistent with the theory that there are multiple instances of the servlet, and init() is being called once on each of them. I'm not sure if you understand precisely what synchronization accomplishes, but in any event it's perfectly legal for a synchronized member method to be called simultaneously on two different objects -- synchronization only prevents it from being called at the same time on the same object.
In any case, so it sounds like you're writing code in init() that must not be called more than one in the entire JVM, and then you're surprised that it's in fact being called multiple times. You need to do something like this:
The static flag ensures that initLAP() will only be called once, ever.
I think there is still a very small chance that two thread will both get a false value for the flag. so both thread will go a head call initLap(). However, if you configure your web app so that the servlet is loaded when server is staring up. It is gurranteed that the init() method will be called while no other thread is looking at the flag.
[ EJFH: Edited for format so page width won't be so wide. ] [ July 06, 2004: Message edited by: Ernest Friedman-Hill ]
there is still a very small chance that two thread will both get a false value for the flag
My above statment was wrong as I miss read the synchronized key word. However, it is better statically invoke the logic of initLap instead of calling them in init() mehtod because then you don't need synchronize the method.