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.

  • 相关阅读:
    字符串-串的最大表示-后缀数组-1163. 按字典序排在最后的子串
    动态规划-买卖股票的最佳时机 V
    贪心-到达终点数字
    贪心-优先队列-模拟-任务调度器
    滑动窗口-区间统计
    快速排序-无序数组K小元素
    POJ 2299 Ultra-QuickSort【树状数组 ,逆序数】
    POJ 2976 Dropping tests【二分 最大化平均值】
    POJ 1064 Cable master 【二分答案】
    POJ 3190 Stall Reservations 【贪心 优先队列】
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2839640.html
Copyright © 2011-2022 走看看