Search code examples
javamultithreadingsynchronizationconcurrenthashmap

Concurrency in java web application


I read lot of blogs and articles that ask to take care while coding for synchronization. They ask to use concurrentHashMap, synchronizedList etc.

As per my understanding, in java web application, Application server (e.g. jboss, weblogic, tomcat), every request run under a separate thread.

e.g. I have sequence of method execution method1--> method2--> method3, then every request will have its own execution stack. Then why do we need to think more about synchronization?

Either my understanding about concurrent request is not correct or I am missing something about synchronization scenarios.

Please advise.


Solution

  • Because in most applications, processing a request involves accessing some shared data that may also be accessed by other threads handling other requests at the same time.

    HTTP sessions are a prime example: all requests in the same session share the same HttpSession object, so if a browser sends two requests at the same time, the two threads handling those requests may try to access the same HttpSession object at the same time. You need synchronization to avoid corrupting the session.