Servlet instances are inherently not thread safe because of the multi threaded nature of the Java programming language in general. The Java Virtual Machine supports executing the same code by multiple threads. This is a great performance benefit on machines which have multiple processors. This also allows the same code to be executed by multiple concurrent users without blocking each other.
Servlet实例天生就是非线程安全的,由于Java的多线程特性。JVM支持在多线程中运行同一段代码。这是一个很好的性能优点在拥有多个处理器的机器上。这也容许相同的代码在并发执行下不会阻塞。web
Imagine a server with 4 processors wherein a normal servlet can handle 1000 requests per second. If that servlet were threadsafe, then the web application would act like as if it runs on a server with 1 processor wherein the servlet can handle only 250 requests per second (okay, it's not exactly like that, but you got the idea).
想象一个有4个处理器的服务器在一个正常的servlet能够处理每秒1000个请求。若是servlet是线程安全的,则web应用就像运行在一秒只能处理250个请求的单处理器机器上(是的,虽然不是彻底同样,可是你应该理解了)。安全
If you encounter threadsafety issues when using servlets, then it is your fault, not Java's nor Servlet's fault. You'd need to fix the servlet code as such that request or session scoped data is never assigned as an instance variable of the servlet. For an in-depth explanation, see also How do servlets work? Instantiation, session variables and multithreading.
若是你在使用servlets的时候你遇到了线程问题,那是你的错误,而不是Java的错误更不是Servlets的错误。你须要修改Servlet中的如:不要将request范围或session范围的实例变量定义在Servlet对象上。深刻解释下请参考 How do servlets work? Instantiation, sessions, shared variables and multithreading服务器