Servlet singlethreadmodel

It is recommended that a developer take other means to resolve those issues instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources.

The Single Thread Model Interface is deprecated in this version of the specification.

Servlet singlethreadmodel-4Servlet singlethreadmodel-26

Other concurrent requests would wait until the current request was processed before starting execution.

If your servlet received an average of one request per second and the processing took an average of half a second, then this implementation is adequate.

You could have plenty of spare CPU power; indeed, if you had eight CPUs, this implementation would leave seven of them mostly idle.

The result is that the server listen queue would fill up quickly, and your servlet (actually, the TCP stack) would simply reject half the connections.

Note that Single Thread Model does not solve all thread safety issues.

For example, session attributes and static variables can still be accessed by multiple requests on multiple threads at the same time, even when Single Thread Model servlets are used.It is strongly recommended that Developers not synchronize the service method (or methods dispatched to it) in these circumstances because of detrimental effects on performance".Although it is standard to have one servlet instance per registered servlet name, it is possible for a servlet to elect instead to have a pool of instances created for each of its names, all sharing the duty of handling requests.Writing your servlet with big synchronized blocks may be highly thread-safe but won't scale.For example, the following rather extreme implementation synchronizes the entire servlet activity: With this servlet implementation, every HTTP request processed by this servlet would have to go through the same synchronized monitor from start to finish, so only one request could be processed at a time, regardless of how many threads you spawned for this servlet.Such servlets indicate this desire by implementing the servlet must guarantee, according to the Servlet API documentation, "that no two threads will execute concurrently the service method of that servlet." To accomplish this, each thread uses a free servlet instance from the pool, as shown in Figure 3-4.