aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Synchronizing service method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Synchronizing service method" Watch "Synchronizing service method" New topic
Author

Synchronizing service method

Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
Hello,
I was reading book on SCWCD it says that if we make doGet or doPost method of the servlet synchronzed than server can serve only one request at a time(even if it has multiple instances). Can anybody explain this as service methid is instance method and on synchornizing it there will be instance lock and not a class lock. So other instance of the servlet should be allowed to invoke that method. Please explain.


Prakash Dwivedi (SCJP2, SCWCD, SCBCD)
"Failure is not when you fall down, Its only when you don't get up again"
Faisal Khan
Ranch Hand

Joined: Jun 29, 2003
Posts: 285
Servlets only ever have one instance (unless of course they implement the SingleThreadModel) therefore if the service method is synchronized, new requests (i.e. new threads) will have to wait for the previous thread to finish.
HTH


The secret to creativity is knowing how to hide your sources.
Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
Originally posted by Faisal Khan:
Servlets only ever have one instance (unless of course they implement the SingleThreadModel)
HTH

Thanks Faisal
I think wrox Prfessional J2EE 1.3 Programming says that a servlet container may create more then one instance of a servelt(no restrictions of SingleThreadModel was mentioned). It also says that to optimize performance it is not uncommon for servlet container to create more than one instance. Also i dont think anywhere in Sun's specification they have said that a servlet container can create more than one instance only when the servelt is implementing SingleThreadModel and not otherwise. Correct me if i am wrong.

thanks
amol deshpande
Ranch Hand

Joined: Sep 05, 2003
Posts: 162
Hello friends,
lets see if that helps.
1) Servlet container generally keeps a single instance of a servlet on memory, but its not a binding by specification.
For optimization purpose, it may create another instance, and do the processsing.
2) Implementing SingleThreadModel is one way to keep only a single worker thread executing a method on servlet instance, but if container decides to create another instance , then still many intances may work in tandom and may be called upon my worker threads (one at a time). So , single thread model doesnt mandate only a single instance.
SingleThreadModel doesnt mean single thread model of a servlet instance...its not a thread. It means single thread model of worker threads....if its singlethread model, a single worker thread will model on an instance.

3) On this I agree with Prakash. A synchronized method will ensure that a single thread works on the method. But if more instances are there, those many mehtods can be served at a time. Even I had this doubt in mind when I was studing, but while execution I didnt found any different situation,
please elaborate on this if possible.
When a method is synchronized, does it have only one existance(as static method) eventhough many instances are there? If yes then book is right, but if not then it can be simult. executed.

Thanks
amol
scjp
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
In the multi-threaded approach where a servlet not hosted in a distributed environment, the servlet container must use only one instance per servlet declaration. For each servlet request, the container spawns a separate thread which executes the servlet's service method.
If you synchronize the service method (or methods such as doGet or doPost which are dispatched to the service method of the HttpServlet abstract class), the servlet container must serialize requests through it. Therefore this approach is not recommended for performance reasons.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Synchronizing service method