Redian新闻
>
Tomcat Servlet: synchronized vs non-synchronized methods
avatar
Tomcat Servlet: synchronized vs non-synchronized methods# Java - 爪哇娇娃
l*b
1
am testing the servlet application using tomcat and customized multithreded
test clients. I try the following different scenarios - synchronize or not
the servlet method. In client side, I generate 200 concurrent requests to
this servlet. There are quite different CPU usage in this scenarios: non-
synchronized one use much more CPU (almost 100%) than synchronized (as usual
). Could anyone advise me what's happened there and why has that different?
e.g.
public TestServlet .....{
public void doGet
avatar
m*t
2

multithreded
usual
Execution in handleRequest() is serialized with
the synchronized version, hence the low cpu utilization.

【在 l*****b 的大作中提到】
: am testing the servlet application using tomcat and customized multithreded
: test clients. I try the following different scenarios - synchronize or not
: the servlet method. In client side, I generate 200 concurrent requests to
: this servlet. There are quite different CPU usage in this scenarios: non-
: synchronized one use much more CPU (almost 100%) than synchronized (as usual
: ). Could anyone advise me what's happened there and why has that different?
: e.g.
: public TestServlet .....{
: public void doGet

avatar
l*b
3
Hi, magicfat , thank you. Many suggest to take "synchronized" out. My
Servlet has no any instance or static variable. Could I assume the un-
synchronized method is still thread safe? And this high CPU usage is normal
case for un-synchronized method?
avatar
F*n
4
Why you so care about high CPU usage? It means your requests are handled
concurrently and therefore faster. Servlet should not have state in most
cases, so there is really no need to make it thread safe. If you need to
access some stateful data, put them into some objects and make them thread
safe.

normal

【在 l*****b 的大作中提到】
: Hi, magicfat , thank you. Many suggest to take "synchronized" out. My
: Servlet has no any instance or static variable. Could I assume the un-
: synchronized method is still thread safe? And this high CPU usage is normal
: case for un-synchronized method?

avatar
l*b
5
Hi, all, many thanks. Your advice is helpful indeed. BTW, I am a performance
zealot.
avatar
F*n
6
But high CPU usage actually means it's faster (No waiting line!)

performance

【在 l*****b 的大作中提到】
: Hi, all, many thanks. Your advice is helpful indeed. BTW, I am a performance
: zealot.

avatar
m*t
7

performance
Well, as Foxman said, high CPU utilization is
A Good Thing(tm). 8-)
(Not too high though, you want to keep the running
average under 50% or even lower)

【在 l*****b 的大作中提到】
: Hi, all, many thanks. Your advice is helpful indeed. BTW, I am a performance
: zealot.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。