File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Servlets and Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Servlets and Threads" Watch "Servlets and Threads" New topic
Author

Servlets and Threads

Simon Harvey
Ranch Hand

Joined: Jan 26, 2003
Posts: 79
Hi everyone,
Is the only time I need to synchronise a method in a servlet, when that method accesses a global variable at the top of the servlet.
For example this Wouldnt need sync'd

I'm trying to figure out why most methods dont need to be synchronised. The only thing I can think of is if its only when they access global variables that things might go wrong.
Is it the case that most methods are ok, because when a context switch happens, the internal variables for the method are stored and reiniitalised for when the other thread comes back online
Thanks to anyone who can help
Simon
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1863
hi simon
yes. u r right in ur predictions. the methods using a local vars need not be synchronized. its only when we have a instance variables in the Servlet class which are used by methods as that variable will be shared for each thread spawned for the servlet.
tho, i have not used much of such instance vars in a servlet to avoid those synchronization issues BUT sometimes it might be "must" to have those and cope with synchronized issues.
regards
maulin


1. Have fun @ http://faq.javaranch.com/java/JavaRaq
2. Looking for simple infix2postfix conversion and postfix evaluation package? Click here
 
IntelliJ Java IDE
 
subject: Servlets and Threads
 
Threads others viewed
Using instance variables
Global variables
Is This program wrong according to Java?
static methods/casting question
Parameters from JSP to Main method of a Class
IntelliJ Java IDE