Servlet Threads

In the AuditFilter example, several lines of code were encased in a synchronize block. This was done to ensure that the counter would be correctly incremented if more than one access is made to the page at the same time. Without the synchronize block, it is possible for two (or possibly more) servlet threads to obtain the counter value before it has been incremented, thereby losing one of the increments. In this case, failing to count one page hit may not be a significant problem. There are other situations where inconsistent data updates may be important.

Shared resources, such as files and databases, can also present concurrency issues when accessed by more than one servlet at a time. Servlets must be written to be multi-thread safe. In simple terms any instance variables and other shared objects must be accessed using synchronized code blocks (as shown in the previous filter example). This approach is illustrated in the later section on the “Agency Case Study.”

NOTE

Prior to version 2.4 of the servlet specification, an interface called SingleThreadModel could be implemented by a servlet to indicate that it was not multi-thread safe. This interface has been deprecated and should no longer be used. Implementing SingleThreadModel did not resolve multi-threading problems when accessing shared objects such as those kept in the servlet context or HTTP session objects and led programmers into a false sense of security.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.239.182