Well, the servlet engine is itself thredsafe, so you don't need to make your servlets threadsafe. IF you want only a single thread to access your servlet you can use the single-thread model (wel explained in Servlet Programming, O'Reilly). Hope this helps.
Hi, The servlet engine spawns new threads that handle client requests. But, if you have a shared resource like a Database or file or some variable in the servletcontext, then, you need to make that piece of code thread-safe. The simplest alternative is make the servlet implement the SingleThreadModel. This adds the restriction that the servlet engine will execute all client requests for this servlet in only 1 thread. But, this drastically reduces the concurrency and response time will increase exponentially. The other and a more practical alternative is to add synchronized blocks around the code that should run "atomically" i.e. in a single go. Hope this helps. Ashwin.
You make your servlet thread safe the way you make any other class thread safe. 1. Use local variables for any parameters that might be changed by a thread. For example, each request to a servlet has an output stream that is used to send the response back to the client. Since each client is different, there will be a different output stream for each client. Thus, the variable that references the output stream should not be a class member, but should be a method local variable. 2. Use member variables for information that is the same for every request, or must be global for a request. When you access or change this variable, protect the access by using a synchronized block of code. For (a very contrived) example, suppose you want to restrict the number of concurrent accesses to your servlet. You might use a member variable to count the number of threads currently running in the servlet. Since two threads can access and update this variable "simultaneously," you would probably want to put a synchronized block around this variable so that only a single thread can access and increment/decrement the variable at a time. 3. Synchronize the smallest amount of code possible. Never synchronize service(), doGet(), or doPost(). Synchronizing service(), doGet(), or doPost() essentially makes your servlet single threaded, without even the benefit of SingleThreadModel which can still handle concurrent requests. 4. If you use external resources which are shared by threads, you must protect the access to those resources using synchronized blocks or methods. Using a local variable is not enough to protect those resources if the threads use the same resource.
A servlet container is free to create and "init" as many copies of a servlet as it likes. Usually a servlet container will create one copy, and run as many threads through it as are necessary. For this to work, you have to make sure your servlet is threadsafe - don't use static or member variables for request-specific values, for instance. If you can't make your servlet threadsafe (usually because you are using non-threadsafe library code), then you must indicate this to the servlet container by adding "implements SingleThreadModel" to the servlet class definition. When a servlet container sees this, it will only route one thread through the servlet at any one time. It may do this by creating more than one copy of the servlet, or by queueing requests for the servlet, or by refusing requests when the servlet is busy. A servlet marked as SingleThreadModel almost always runs more slowly than a multi-threaded one, so making sure servlets are threadsafe is very important.