ActionServlet - 同步块 - 线程的工作
如果在service()
方法中有一个synchronized
块,例如在struts的ActionServlet
中,多个请求/线程将如何工作这是一个繁忙的网站,有大量的点击量。
每个线程是否会等待下一个线程从同步块中释放锁?这会造成响应延迟吗
if there is a synchronized
block in the service()
method of say for example in ActionServlet
of struts, how will multiple requests/threads work if it is a busy site having large number of hits.
Will each thread wait for the next other one to release lock from the synchronized block? Will that create delay in response
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要同步 servlet 的 service 方法。
如果您同步 servlet 的
service
方法,您实际上是在为位于以下位置的线程进行“访问预留”该 Servlet 实例的时间。Struts
ActionServlet
类是一个HttpServlet
,基本上这里感兴趣的是doGet
和doPost
方法。如果我们要谈论 Struts,process 方法是主要入口点,但与一般服务相同的原则适用于所有方法方法。
想法是这样的。
当您在
web.app
中声明 servlet 时,servlet 容器(例如 Tomcat)将仅创建该 servlet 的一个实例。这意味着只有一个实例可以满足所有请求。如果更多请求同时到达,则每个请求线程都有机会使用
service
方法,因为没有强制执行同步。如果您有 10 个请求线程,每个请求线程都将在
service
方法中并发执行。这通常是安全的,因为在service
方法中完成的处理不涉及与其正在处理的当前请求相关的任何状态。如果您向 servlet 添加状态,就会遇到问题。 这是一篇有关该主题的更多详细信息的文章。现在回到 Struts。
Struts 使用名为 前端控制器 的模式以及
ActionServlet 是那个控制器。这将依次将特定请求委托给其配置(又名
struts-config.xml
)中指定的特定Action
类。所有传入的请求都通过这里。如果您在此时放置同步(Struts
process
方法或更高层的 servletservice
方法),您将一次为一个线程保留 servlet。 对于 struts,您一次将请求的所有处理保留给一个线程。这意味着如果 10 个请求同时到达,在没有同步的情况下,所有请求都可以并行执行,而在同步的情况下,请求 2 必须等待,直到请求 1 完成,3 等待 2 等等(即请求是依次处理)。这意味着性能不佳。
也许对于提出请求 1 的幸运用户来说,不会对性能产生影响,但请求 10 则必须等待。那么100号呢? 200?
如果您在编写应用程序时考虑到线程安全,则无需同步入口点。如果您的应用程序属于无法避免同步的类型,那么同步入口点将会降低性能。
PS 只是另一件事。如果您正在考虑将同步移到流程中的较低位置,即
Action
类,请注意它们也不是线程安全的,并且有 Struts 框架内只有一个实例。Don't synchronize the service method of a servlet.
If you synchronize the
service
method of a servlet, you are in fact making a "reservation on access" for a thread at a time for that instance of the Servlet.The Struts
ActionServlet
class is aHttpServlet
and basically thedoGet
anddoPost
methods are of interest here. If we are to speak about Struts, the process method is the main entry point, but the same principle applies to all methods as it does for the generalservice
method.The idea is this.
When you declare a servlet in your
web.app
, the servlet container (e.g. Tomcat) will create only one instance of that servlet. This means there is only one instance to serve all requests.If more requests arrive at the same time, each request thread gets it chance at the
service
method because there is no synchronization enforced.If you have 10 request threads, each will execute concurrently in the
service
method. This is normally safe because processing done in theservice
method does not involve any state related to the current request it is handling. You go into issues if you add state to your servlets. Here is an article with more details on the subject.Now back to Struts.
Struts uses a pattern called a Front Controller with the
ActionServlet
being that controller. This will in turn delegate specific requests to specificAction
classes as specified in its configuration (a.k.astruts-config.xml
).All incoming request pass though here. If you place synchronization at this point (the Struts
process
method or the servletservice
method higher up) you are reserving the servlet for a thread at a time. In case of struts, you are reserving all processing of a request to a single thread at a time.That means that if 10 request arrive simultaneousely, in the case without synchronization all can execute side by side, while in the case with synchronization request 2 will have to wait until request 1 is done, 3 waits for 2 and so on (i.e. requests are sequentially processed). And this means poor performance.
Maybe for the lucky user that made request 1 there will be no performance impact but number 10 will have to wait. Then how about number 100? 200?
There is no need to synchronize the entry point if you program your application with thread safety in mind. If your application is of the sort that you just can't avoid synchronization, then synchronizing the entry point will reduce performance.
P.S. Just one other thing. If you are thinking into moving the synchronization lower in the process, namely the
Action
classes, note that they are not thread safe either and there is only one instance of it inside the Struts framework.