*
The moose likes Servlets and the fly likes Servlet and syncrhonized block. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet and syncrhonized block." Watch "Servlet and syncrhonized block." New topic
Author

Servlet and syncrhonized block.

Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
Any request to the servlet will spawn a new thread a separate copy of a servlet will be running is that true ? If so what if i have a syncrhonized block in a servlets doGet() which does some lengthy operation , will it block any other request to this servlet until i leave the sync block ? or is it just for that servlets request for that user ? I need your help on this , thanks.
Val.


Val SCJP <BR>going for SCJD
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Yes, any other access to the synchronized block will be blocked intil the lock the first thread holds on the block of code is released.
Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
"Any request to the servlet will spawn a new thread a separate copy of a servlet will be running is that true ?"
NO - the same copy of the servlet object will be shared by all Threads. That is why you must be very carefull about using instance variables - they are visible to all Threads.
If you can't figure out a way to share a resource, you can synchronize access. Synchronizing the entire method is safest. If you synchronize a block, be sure to synchronize on an object that is visible to all Threads - synchronizing on a local object is useless.
Bill
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by William Brogden:
If you can't figure out a way to share a resource, you can synchronize access.

... or implement SingleThreadModel.
- Peter
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
i needed a way to control doGet method so i won't leave it. so using synchronized block was a good idea for me. I still want the servlet to be multi threaded. Thanks for your input.
 
jQuery in Action, 2nd edition
 
subject: Servlet and syncrhonized block.
 
Similar Threads
changing http get request to post request
ServletRequestListener problem
sockets and browser issues.
ServletContext & synchronization?
socket and servlet.