• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronizing getServletContext

 
Raef Kandeel
Ranch Hand
Posts: 87
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because you have to synchronize on the getServletContext() in the second servlet too.
 
NoviceChandra Manish
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic