File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Does synchronized works in a servlet init function? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Does synchronized works in a servlet init function?" Watch "Does synchronized works in a servlet init function?" New topic
Author

Does synchronized works in a servlet init function?

gooi chin hin
Greenhorn

Joined: Dec 03, 2002
Posts: 9
I have been trying to call a Synchronized function in the servlet init function but it doesn't seem to be thread safe.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

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?


[Jess in Action][AskingGoodQuestions]
Nagaraja Manchikalapati
Greenhorn

Joined: Jun 22, 2004
Posts: 8
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.

-Nagaraj
gooi chin hin
Greenhorn

Joined: Dec 03, 2002
Posts: 9
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.

Thanks
Natarajan Shankar
Ranch Hand

Joined: Jun 10, 2004
Posts: 53
Can you please check the code are you calling init() in your program. And also possible can you try with synchronize block also.


Best Regards<br />N.Shankar<br />SCJP, SCWCD
gooi chin hin
Greenhorn

Joined: Dec 03, 2002
Posts: 9
Yup. I am sure that I am calling the synchronize function inside the
servlet init() function. Any solutions???

Thanks.
gooi chin hin
Greenhorn

Joined: Dec 03, 2002
Posts: 9
Hi..
Anybody out there can help me?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

What evidence do you have that the synchronized function is being called multiple times simultaneously, as opposed to sequentially?

It is true that init() should only be called once on a given servlet object; but there may be multiple instances of your servlet class.

As I said before, tell us in detail what you did, what you expected to happen, and what happened instead, being as specific as you can, and giving us only facts, not your guesses or explanations.
Vijay Venkat
Ranch Hand

Joined: Aug 12, 2001
Posts: 52
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
Greenhorn

Joined: Dec 03, 2002
Posts: 9
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.

Regards,
Gooi
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.
Jack Zhou
Ranch Hand

Joined: Dec 27, 2003
Posts: 93
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 ]

Thanks,<br />Jack Zhou<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS,SCEA
Jack Zhou
Ranch Hand

Joined: Dec 27, 2003
Posts: 93
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.
Johan Rylander
Greenhorn

Joined: Jul 13, 2004
Posts: 1
Servlet may be instantiated several times, the synchronized keyword without stating a certain object synchronizes on each instance. Multiple instances, multiple calls.

Originally posted by gooi chin hin:
Yup. I am sure that I am calling the synchronize function inside the
servlet init() function. Any solutions???

Thanks.
 
 
subject: Does synchronized works in a servlet init function?