I know this is a simple question, but somehow I am confused.
If I understand well, in simple words, when a request arrives at a web server, it creates a stream for each request to some servlet.
We believe that we have the following code in MyServlet (I did not consider exception handling, etc.):
synchronized protected void doGet( ... ...){ PrintWritet pw=response.getWriter(); String param=request.getParameter("p"); if(param.equals("a")){ wait(); }else{ notifyAll(); } pw.write("Hello!"); }
I expect this servlet to get stuck because the first thread (with parameter = a) that goes into this method will wait indefinitely, because any other future thread will get stuck before doGet because of a synchronized keyword, and because of that notifyAll will never be executed.
Now, if I open a new tab in the browser and hit / MyServlet? p = a, browser Waiting for 127.0.0.1 ... After that I open a new tab and hit / MyServlet? P = b (or something that is! = A) The first tab is released and "Hello!" Is printed. message.
This means that the second thread introduced doGet and executed notifyAll.
Why is this happening? What did I miss?
source share