zoukankan      html  css  js  c++  java
  • Why javax.servlet Interface SingleThreadModel is deprecated? How to handle the concurrency in servlet?

    note : this is from jave doc.

    What is the SingleThreadModel for?

    Ensures that servlets handle only one request at a time. This interface has no methods.

    If a servlet implements this interface, you are guaranteed that no two threads will execute concurrently in the servlet's service method. The servlet container can make this guarantee by synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet.

    Why deprecated?

    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. This interface is deprecated in Servlet API version 2.4.

    What is the recommended way to handle concurrent in 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.

  • 相关阅读:
    POJ 2175 Evacuation Plan 费用流 负圈定理
    POJ 2983 Is the Information Reliable? 差分约束
    codeforces 420B Online Meeting
    POJ 3181 Dollar Dayz DP
    POJ Ant Counting DP
    POJ 1742 Coins DP 01背包
    中国儒学史
    产品思维30讲
    Java多线程编程核心技术
    编写高质量代码:改善Java程序的151个建议
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2839640.html
Copyright © 2011-2022 走看看