11

Java Servlet life cycle is managed by the servlet container. When first web request comes in, the container will load the Servlet class, calls its init method, then calls its service method to process the web request. It says there can be only one instance of servlet class. The container creates multiple threads and manages these threads to process multiple web requests (this is what I know from my knowledge). But I want to understand, how multiple threads are running and processing multiple simultaneous web requests given that there is only one instance of the servlet class. Can some one shed light on this?

1
  • 1
    Just because there's only a single servlet instance doesn't mean multiple threads of execution can't call that servlet's methods. Oct 9, 2011 at 6:28

2 Answers 2

18

An object instance('s methods) can be called simultaneously by more than one thread. This isn't specific for servlets though and is true in general.

What happens when this occurs? Each thread will still have their own stack, which means, each thread will have a different copy of local variables to work on. As a result there will be no interference between threads and you won't have to worry about concurrent calls. Only when a shared resource, e.g. instance/class variable is accessed, there can be a problem. Same thing if instance/class variable is directly accessed concurrently.

EJBs in contrast do exactly what you seem to suggest. The EJB container makes sure that only one thread enters an EJB instance at a time, and hence an EJB programmer doesn't have to worry about concurrency so long as he/she doesn't break the EJB programming contract. There is no reason why the servlet spec didn't do this. Most likely no body came up with it during the meetings? It does have the advantage though that you can use a more efficient concurrency management than EJB's "one thread per instance".

2
  • In fact, current design is much better because instance variables do not need to be created multiple times (one per request). Even if the instance variable is something like logger, or handle to the data service, it can simply be shared. Jun 20, 2014 at 14:58
  • So in servlets service method, any shared resource, e.g : file handler, the variable for data base access, need to be properly protected for multi-thread access ?
    – bubu0321
    Jun 16, 2019 at 2:05
5

The servlet is protected from concurrent call until the initialisation is complete. Once this has been done, all calls are executed simutaneously on your servlet. This means the service method can be invoked by two concurrent threads if two requests need to be processed at the same time.

If this behaviour doesn't suit you (for example, if you are using some kind of non-thread safe resource) and are really sure you don't want your method to be called by multiple threads concurrently, you can mark your service method as synchronized.

1
  • 1
    I don't think it would be a good idea to synchronize on the service method that would mean that the servlet only processes one request at a time and this just a recipe for a very slow web app...rather, it's better to synchronize on the resource that is being accessed by multiple threads...
    – Mikematic
    Nov 17, 2017 at 22:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.