Choose the correct one:
a.) MyServlet is thread safe.
b.) MyServlet is not thread safe because myName is an instance variable.
c.) MyServlet is not thread safe because MyServlet implements SingleThreadModel.
d.) None of the above.
Choice A is correct. An application is thread safe if it always behaves predictably regardless of the number of concurrent threads running in its process space. The simplest way to ensure that a servlet is thread safe is to implement the SingleThreadModel interface. By implementing this interface, the server guarantees that no more than one thread can execute the service(), doGet(), or doPost() method at a time for a particular servlet instance. This makes the servlet thread safe. Thus even if class MyServlet has instance variables, it is thread safe. Thus A is the correct choice and the other choices are incorrect.
What if container initiates a second instance of same servlet... No two threads can simultanously access a single instance but two threads can access two separate instances. As each instance will want to work on same attribute, this might render itself thread unsafe...
[ November 08, 2003: Message edited by: Shiva Mantri ]