Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Does synchronized works in a servlet init function?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been trying to call a Synchronized function in the servlet init function but it doesn't seem to be thread safe.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please check the code are you calling init() in your program. And also possible can you try with synchronize block also.
 
gooi chin hin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. I am sure that I am calling the synchronize function inside the
servlet init() function. Any solutions???

Thanks.
 
gooi chin hin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..
Anybody out there can help me?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jack Zhou
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
It's a tiny ad only because the water is so cold.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic