Why does a servlet container synchronise access for multiple requests to a particular resource/ servlet?

别来无恙 提交于 2019-12-24 17:25:40

问题


By default, the servlet container loads only a single instance of a servlet. Requests serviced by the servlet are run in separate threads, but share the same instance. This enables applications to be responsive and scalable, since it requires fewer resources and uses them efficiently.

Almost everyone knows that this is the default threading model.

This question/discussion is a follow-up on this particular stackoverflow thread.

But the output happens to be completely different in my case,

May 21, 2016 1:57:59 PM com.stackoverflow.question8011138.MyServlet doGet
INFO: [doGet] Test before                        // 1st  thread

May 21, 2016 1:58:26 PM com.stackoverflow.question8011138.MyServlet doGet
INFO: [doGet] Test before                        // 2nd  thread

May 21, 2016 1:58:59 PM com.stackoverflow.question8011138.MyServlet doGet
INFO: [doGet] Test after                         // 1st  thread

May 21, 2016 1:59:26 PM com.stackoverflow.question8011138.MyServlet doGet
INFO: [doGet] Test after                         // 2nd  thread

Even though I hit the url mapped to this particlular servlet in 2 browser windows of the same browser, the difference b/w these 2 hitting ~1 second, but I see the below output

May 21, 2016 1:58:26 PM com.stackoverflow.question8011138.MyServlet doGet
INFO: [doGet] Test before                        // 2nd  thread

after seconds more than that previously mentioned ~1 second,

i.e to this,

May 21, 2016 1:57:59 PM com.stackoverflow.question8011138.MyServlet doGet
INFO: [doGet] Test before                        // 1st  thread

Why?

Clearly, from the output above, the second request is not served after the first has been served completely, but in b/w, contrary to what the OP to that question suggested.

What exactly is going on?

Why 2 different outputs to the same scenario?

The result/ output clearly shows that the server is not synchronizing the 2 requests.

来源:https://stackoverflow.com/questions/37360731/why-does-a-servlet-container-synchronise-access-for-multiple-requests-to-a-parti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!