ch3.sharedVariable.error
Class Controller
java.lang.Object
javax.servlet.GenericServlet
javax.servlet.http.HttpServlet
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. |
|
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 |
accessCount
public int accessCount
- The variable that is being incremented.
Controller
public Controller()
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 objectresponse - 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.
- Copy to a register (temp = accessCount)
- Increment the register (temp++)
- 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.