Example of a servlet with synchronized service method
public class TestServlet implements Servlet {
// --- Multiple state varibles
public synchronized void service(ServletRequest req,
ServletResponse resp) {
ServletResponse resp) {
// -----
// -----
}
}
This above example, only one thread can execute service method at once, which can result in frustrated users if load is higher.
If system has multiple CPUs, processors may remain idle even on the high load.
Multiple requests will queue up and handled sequentially due to poor concurrency.
A ->
B ->
C ->
Solution
Atomic variables are effective only for atomic operations on a single variable.
To improve the concurrency in the servlet, narrow the synchronize block.
Avoid holding locks during lengthy computations or operations at risk of not completing quickly.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.