SingleThreadModel does not even guarantee thread safety, since the resulting servlet instances can still access classes and data at the same time.
However, SingleThreadModel does guarantee that more resources will be used than are needed, as maintaining the multiple instances has some cost to the servlet engine.
Instead of using SingleThreadModel , concentrate on writing a thread-safe multi-threaded servlet. See Section 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.
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. However, if your servlet received an average of one request per second and the processing took an average of two seconds, then your servlet can process an average of only one request every two secondsi.
This is independent of the CPU capability of the server. 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.
To sum up:. Try to minimize the amount of time spent in synchronized code while still maintaining a thread-safe servlet.
Where a limited number of services must be distributed among the servlet threads for example, database connections , use resource pools such as database connection pools to provide optimal service distribution. All Rights Reserved. Use is subject to license terms. Skip navigation links. BalusC 1. Add a comment. Active Oldest Votes. SingleThreadModel was designed to be an easy solution to low-load concurrency, but it didn't even manage that: Note that SingleThreadModel does not solve all thread safety issues.
If it can't achieve what it was designed for, it should not be used. And STM uses only one thread at a time. I know I am not a senior, but I disagree with this. Could you correct me? Note: SingleThreadModel has been deprecated since Servlet 2. Jon Skeet Jon Skeet 1. Sign up or log in Sign up using Google. Sign up using Facebook.
Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Explaining the semiconductor shortage, and how it might end. Does ES6 make JavaScript frameworks obsolete? Featured on Meta.
0コメント