Context: I'm reading "Java Concurrency in Practice" by Brian Goetz and others (sorry others). On the beginning of my copy there is
@ThreadSafe
public class StatelessFactorizer implements Servlet {
public void service(ServletRequest req, ServletResponse resp) {
BigInteger i = extractFromRequest(req);
BigInteger[] factors = factor(i);
encodeIntoResponse(resp, factors);
}
}
(code sample from author is here however in book it looks as I added it above)
My quesiton is: while the 'service' method has some external references to some objects it potentially can do sth with it, for example change it's state. Thus if 2 threads call 'service' at the same time, that state will be modified from 2 places and the class is not thread safe. A I right?
The book I mentioned is about concurrency. Reader can know absolutely nothing about HTTP and/or Servlet. My instance of the book does not even state what is the 'Servlet' the 'StatelessFactorizer' implements. So as it can be anything, there can be no assumption like "Your methods are using different objects which shouldn't effect other requests". I mean you don't know what 'extractFromRequest(req)' and 'encodeIntoResponse(resp, factors)' do. As the chapter deliberates about stateless I assume the chapter is Servlet-agnostic.