aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Synchronizing getServletContext 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 getServletContext" Watch "Synchronizing getServletContext" New topic
Author

Synchronizing getServletContext

Raef Kandeel
Ranch Hand

Joined: Aug 05, 2007
Posts: 87
Hey Ranchers,

long time no see. After failing my first attempt in scwcd, I am trying some of the exercises in the headfirst book. I hope you are kind enough to help me out. I tried this code out:-



When the thread is sleeping I would waken up my second servlet. It's code is:-



I expected that the second servlet would have to wait for the first servlet to finish, and then the first servlet would print out: 22, 42, but however, the second servlet did not wait for the first servlet and the output was: 22 16. I tested it out on Tomcat 5.0. Do you know why?


SCJP, SCWCD
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
because you have to synchronize on the getServletContext() in the second servlet too.


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
NoviceChandra Manish
Greenhorn

Joined: Aug 14, 2014
Posts: 2
Omar Al Kababji wrote:because you have to synchronize on the getServletContext() in the second servlet too.



But in a servlet container for a particular web Application there exist only one context object per JVM, and if there is a lock anywhere in web app on the servlet context , then same lock will be on anywhere else , whereever that context is being used by any other request or thread. Please correct if I am wrong
NoviceChandra Manish
Greenhorn

Joined: Aug 14, 2014
Posts: 2
Raef Kandeel wrote:Hey Ranchers,

long time no see. After failing my first attempt in scwcd, I am trying some of the exercises in the headfirst book. I hope you are kind enough to help me out. I tried this code out:-



When the thread is sleeping I would waken up my second servlet. It's code is:-



I expected that the second servlet would have to wait for the first servlet to finish, and then the first servlet would print out: 22, 42, but however, the second servlet did not wait for the first servlet and the output was: 22 16. I tested it out on Tomcat 5.0. Do you know why?



As lock on Servlet Context Object is obtained in servlet and there exist only one servlet per Web App per JVM then this means everywhere , where that servlet Context is used its locked or synchronised. So how come second servlet executes?
Shreyas Dange
Greenhorn

Joined: Jul 30, 2014
Posts: 15

hello manish ..


I expected that the second servlet would have to wait for the first servlet to finish, and then the first servlet would print out: 22, 42, but however, the second servlet did not wait for the first servlet and the output was: 22 16. I tested it out on Tomcat 5.0. Do you know why?




You are totally correct that there is only one ServletContext per vm per web app , but in TestAttributes2 servlet when trying to access a ServletContet object , the code will work completely fine and the modification will take place because the lock is not held on context object from the thread under which TestAtrribute2 is executing , and even in sleep mode the locked context object will be accessible to other servlets and JSP of this web app until they are not using the context object inside a synchronized block with lock on context object ..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronizing getServletContext