ch3.sharedVariable.error
Class Controller

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by ch3.sharedVariable.error.Controller
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig
Direct Known Subclasses:
Controller

public class Controller
extends javax.servlet.http.HttpServlet

A controller that increments a member variable. The addition simulates the distinct steps that the CPU would use to add one to a variable.

If the controller is called from two different browsers within three seconds, then both browsers will receive the same value for the incremented variable.

See Also:
Serialized Form

Field Summary
 int accessCount
          The variable that is being incremented.
 
Constructor Summary
Controller()
           
 
Method Summary
protected  void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Handles the HTTP GET method.
 void incrementSharedVariable()
          Simulate the steps that a CPU performs to increment a variable.
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

accessCount

public int accessCount
The variable that is being incremented.

Constructor Detail

Controller

public Controller()
Method Detail

doGet

protected void doGet(javax.servlet.http.HttpServletRequest request,
                     javax.servlet.http.HttpServletResponse response)
              throws javax.servlet.ServletException,
                     java.io.IOException
Handles the HTTP GET method. It always does the same thing: increments a member variable.

Overrides:
doGet in class javax.servlet.http.HttpServlet
Parameters:
request - Request information encapsulated in a Java object
response - Response information encapsulated in a Java object
Throws:
javax.servlet.ServletException
java.io.IOException

incrementSharedVariable

public void incrementSharedVariable()
Simulate the steps that a CPU performs to increment a variable.
  1. Copy to a register (temp = accessCount)
  2. Increment the register (temp++)
  3. Copy back to memory (accessCount = temp)
After the variable is incremented, the thread is put to sleep for three seconds, to simulate that the thread was interrupted by the CPU.