java 为什么不推荐使用 (javax.servlet.)SingleThreadModel?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2551999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is (javax.servlet.)SingleThreadModel deprecated?
提问by saugata
Why is javax.servlet.SingleThreadModeldeprecated?
回答by skaffman
The javadocsays why. SingleThreadModelwas designed to be an easy solution to low-load concurrency, but it didn't even manage that:
在javadoc中说为什么。SingleThreadModel旨在成为低负载并发的简单解决方案,但它甚至没有做到这一点:
Note that SingleThreadModel does not solve all thread safety issues. For example, session attributes and static variables can still be accessed by multiple requests on multiple threads at the same time, even when SingleThreadModel servlets are used. It is recommended that a developer take other means to resolve those issues instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources.
请注意,SingleThreadModel 并不能解决所有线程安全问题。例如,会话属性和静态变量仍然可以被多个线程上的多个请求同时访问,即使使用 SingleThreadModel servlet。建议开发人员采用其他方式来解决这些问题,而不是实现此接口,例如避免使用实例变量或同步访问这些资源的代码块。
If it can't achieve what it was designed for, it should not be used.
如果它不能实现它的设计目的,就不应该使用它。
回答by Jon Skeet
It's basically a poor way of handling concurrency. Take the state out of your servlet instead, so that the same servlet can be used by multiple threads concurrently. Keeping state in a "pool" of servlet instances, each of which can have state left over from the previous request etc is pretty horrible.
这基本上是一种处理并发的糟糕方式。取而代之的是从 servlet 中取出状态,以便多个线程可以同时使用同一个 servlet。将状态保存在 servlet 实例的“池”中,每个实例都可以具有前一个请求等遗留的状态,这非常可怕。
回答by javaPlease42
Yes SingleThreadModel interface is deprecated. Do not use it.In fact you don't need it, instead use local variables instead of object fields since "each thread gets its own copy of local variables in Java. By simply removing the object field and replacing it with a local variable, this particular threading problem is resolved." Reference
是 SingleThreadModel 接口已弃用。不要使用它。事实上你不需要它,而是使用局部变量而不是对象字段,因为“每个线程在 Java 中都有自己的局部变量副本。通过简单地删除对象字段并用局部变量替换它,这个特殊的线程问题是解决。” 参考
回答by Joe.wang
From Java Servlet Spec:
来自 Java Servlet 规范:
The use of the SingleThreadModel interface guarantees that only one thread at a time will execute in a given servlet instance's service method. It is important to note that this guarantee only applies to each servlet instance, since the container may choose to pool such objects. Objects that are accessible to more than one servlet instance at a time, such as instances of HttpSession, may be available at any particular time to multiple servlets, including those that implement SingleThreadModel.
It is recommended that a developer take other means to resolve those issues instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources. The SingleThreadModel Interface is deprecated in this version of the specification.
SingleThreadModel 接口的使用保证一次只有一个线程将在给定的 servlet 实例的服务方法中执行。需要注意的是,此保证仅适用于每个 servlet 实例,因为容器可能会选择池化此类对象。一次可被多个 servlet 实例访问的对象(例如 HttpSession 的实例)可能在任何特定时间对多个 servlet 可用,包括那些实现 SingleThreadModel 的 servlet。
建议开发人员采用其他方式来解决这些问题,而不是实现此接口,例如避免使用实例变量或同步访问这些资源的代码块。此版本的规范中不推荐使用 SingleThreadModel 接口。
回答by Harun ERGUL
If a servlet implements SingleThreadModelinterface, servlet container can create one or multiple instance of the servlet depend on the request load. Each instance will use only their service()method. It solves thread safety issues but not all of them. Such as static class variables, session attributes are still not thread safe.
如果 servlet 实现了SingleThreadModel接口,servlet 容器可以根据请求负载创建一个或多个 servlet 实例。每个实例将只使用它们的service()方法。它解决了线程安全问题,但不是全部。比如静态类变量,会话属性仍然不是线程安全的。
Instead of using this interface developer encouraged to use synchronizing the block of the code accessing those resources.
鼓励开发人员使用同步访问这些资源的代码块,而不是使用此接口。

