Although servlet developers usually think in terms of servlet instances, it is important to remember that threads, possibly multiple concurrent threads, perform the work.
Because each instance can be running in multiple threads, you must be aware of potential synchronization issues with object-scoped variables and other shared resources. Synchronization ensures that a piece of code executes in a single-threaded manner. You control thread management using the JMC, specifying separate threading parameters for each JVM.
Note: Thread management is an advanced topic, beyond the scope of this book. For more information, see a third-party book on Java thread management.
There are many ways to prevent concurrent access to class-scoped instance variables, including the following:
SingleThreadModel interfacesynchronized keyword to your doXxx method's signature (not recommended)The following subsections discuss these techniques.
You can use the synchronized keyword in the method signature to ensure synchronized access to an entire method:
...
public class TestSync extends HttpServlet
{
��int visitorCounter = 0;
��// Conceptual example only. Don't do this at home.
public synchronized void doGet(HttpServletRequest servReq,
��HttpServletResponse servRes) throws IOException, ServletException {
...
Note: This example is for conceptual purposes only. You almost never use this synchronization technique because of negative performance implications.
You can use a synchronized block to ensure synchronized access to object-scoped variables:
...
int thisCount;
synchronized(this) {
��// visitorCount is an object-scoped variable.
��thisCount = visitorCounter++;
}
out.println("<p>You are visitor number " + thisCount);
...
This example increments the counter inside a synchronized block, which guarantees single-threaded access to the object-scoped variable.
The SingleThreadModel interface instructs JRun to create a pool of servlet instances and ensure, for each instance, that concurrent threads do not execute the service method, as the following example shows:
...
public class testSync extends HttpServlet
��implements SingleThreadModel {
...
Because JRun creates multiple instances of servlets that implement SingleThreadModel, you cannot use this technique when object-scoped instance variables must be consistent for all instances. For example, you cannot use it for a hit counter. However, using SingleThreadModel can be an effective technique for servlets that do not use object-scoped instance variables or when object-scoped instance variables do not have to be consistent, such as with buffer variables or database connections.
You can implement a scheme in which all access to the object-scoped variable is performed by synchronized methods, as the following example shows:
...
public class TestSync extends HttpServlet {
��int visitorCounter = 0;
public void doGet(HttpServletRequest req, HttpServletResponse res)
��throws IOException, ServletException {
����incrementCount();
...
public synchronized void incrementCount() {
��visitorCount++;
}
public synchronized int getCount() {
��return visitorCount;
}
...
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/jrun/4/Programmers_Guide/techniques_servlet15.htm