gpt4 book ai didi

java - 任何人都可以在网络应用程序中详细说明单线程模型吗?

转载 作者:行者123 更新时间:2023-11-29 06:47:41 24 4
gpt4 key购买 nike

在用 java 设计 Web 应用程序时,我们什么时候需要在 Web 应用程序中使用单线程模型。

最佳答案

几乎总是应该避免使用单线程模型。 (我假设您正在谈论 SingleThreadModel 接口(interface)。)基本上引入它是为了让人们不必考虑并发性,但这是一个坏主意。并发性是 Web 应用程序固有的——引入像单线程模型这样的瓶颈是错误的解决方案。正确的解决方案是让开发人员更好地了解并发性,并引入更好的构建 block 来处理它。

从 Java Servlet API 2.4 开始,该接口(interface)已被弃用,并附有以下注释:

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.

关于java - 任何人都可以在网络应用程序中详细说明单线程模型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2011900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com