Plan your text ad campaign.
World class Hard Drive Recovery and renowned raid recovery services
WestNIC provides reliable web hosting services
Free software downloads and drivers download resources
This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQs and more.
A: Yes, servlets are normally multi-threaded. The servlet container allocates a thread for each new request for a single servlet without any special programming. Each thread of your servlet runs as if a single user were accessing it alone, but you can use static variables to store and present information that is common to all threads, like a hit counter for instance.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Servlet containers usually manage concurrent requests by creating a new Java thread for each request. The new thread is given an object reference to the requested servlet, which issues the response through the same thread. This is why it is important to design for concurrency when you write a servlet, because multiple requests may be handled by the same servlet instance.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: The way that servlet containers handle servlet requests is not prescribed to this extent; they may use a single servlet, they may use servlet pooling, it depends on the vendor's system architecture. The point is that you should write your servlet code to take account of a multi-threaded context regardless of the container implementation you happen to be using. In other words, you should adhere to the principle of write once, run anywhere.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Standard servlets are normally handled in a multi-threaded context in the servlet container. A number of virtually simultaneous requests for a single servlet could be handled by different threads using the same servlet instance. Any number of threads could access or modify the static and instance fields of a single servlet instance in an unpredictable sequence. This makes the instance fields of servlets as vulnerable to threading issues as static fields, and modification of shared resources must be considered carefully.
More details available to premium content service subscribers:
What is the single threaded model?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
SingleThreadedModel if servlets are multi-threaded?
A: The SingleThreadedModel interface is used to ensure the safety of servlets that are vulnerable to thread safety issues. Normally, servlets should be written to run safely in a multi-threaded environment, and are executed in this context. The SingleThreadedModel is a way to override the normal multi-threaded execution context of the servlet container.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: To create a servlet that a container will manage on a single threaded basis it must declare that it implements the javax.servlet.SingleThreadModel interface. This is a marker interface, there are no extra methods to fulfil, but this is sufficient for the container to identify the type and manage it accordingly.
public class SingleThreadServlet extends HttpServlet
implements SingleThreadServlet {
// Standard HTTP servlet methods
}
The servlet container will typically synchronize access to a single instance of the servlet, or create a pool of servlet instances and allocate one request per instance.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
SingleThreadedModel?
A: Not unless you have very good reason. The SingleThreadedModel interface has been deprecated, which means that it should not be used and may be removed in a later release of the Java servlet specification and API. It has never really been advisable to implement the SingleThreadedModel, you should always aim to address thread safety issues in servlets using standard Java programming techniques.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
SingleThreadedModel?
A: The SingleThreadedModel servlet specification does not require the creation of a pool of servlets. A single threaded model implementation may also synchronize access to a single servlet instance to achieve the same outcome, so that only one request is processed at a time. Apache Tomcat uses the synchronized approach, other servlet containers may use the pooled approach.
If you are concerned about the performance impact of synchronized access to the servlet instance, it would be preferable to re-design your servlet so that it does not depend on this mechanism at all. For example, you might create a synchronized block around the statements that are vulnerable to threading issues. By doing away with the single threaded model, you will minimise the performance impact of thread control.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
| Front-end FAQs | Back-end FAQs | Learning Java |
|---|---|---|
About us: site help, text ads, sponsored links and premium content.