多个实例时Servlet同步
我读到 Servlet 中的代码可以与同步块同步。然而,我还了解到,虽然 servlet 通常只有一个实例,但 servlet 容器可能会保留一组实例。当然,这意味着同步块不能保证正常工作,因为您不知道请求线程将选择哪个实例?
I have read that code in servlets can be synchronized with synchronized blocks. However, I have also read that whilst there is often only one instance of a servlet the servlet container may keep a pool of instances. Surely this means that a synchronized block is therefore not guaranteed to work as you don't know which instance the request thread will choose?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
规范 (3.0) 的 2.2 节说:
因此,如果容器使用实例池,则违反了规范。我不明白为什么容器会这样做,因为每个 servlet 开发人员都知道多个线程可以同时访问 servlet,因此 servlet 必须是线程安全的。
Section 2.2 of the specification (3.0) says:
So, if a container uses a pool of instances, it's in violation of the spec. I don't see why a container would do that, since every servlet developers know that multiple threads may access the servlet concurrently, and the servlet must thus be thread-safe.