有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (3) 个答案

  1. # 1 楼答案

    如果servlet实现SingleThreadModel接口,servlet容器可以根据请求负载创建一个或多个servlet实例。每个实例将只使用它们的service()方法。它解决了线程安全问题,但不是所有问题。例如静态类变量,会话属性仍然不是线程安全的

    我们鼓励开发人员使用同步代码块来访问这些资源,而不是使用这个接口

  2. # 2 楼答案

    来自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.

  3. # 3 楼答案

    这基本上是一种处理并发性的糟糕方式。取而代之的是,去掉servlet的状态,这样多个线程可以同时使用同一个servlet。将状态保存在servlet实例的“池”中,每个实例都可能有前一个请求遗留下来的状态,这非常可怕