Multi-threading and JSEs

Multi-threading and JSEs

Servlets, and therefore JSEs, support two basic threading models: multi-threaded and single-threaded. To understand these two programming models you have to understand Java threading—if you don't, I recommend you read up on the subject before tackling this section. A good book on threading is the second edition of Concurrent Programming in Java: Design Principles and Patterns, by Doug Lea.

When a JSE is multi-threaded, all client requests access exactly the same JAX-RPC servlet and JSE instance. Each client request is associated with its own thread of execution, and many threads will access the same JSE instance simultaneously. This model allows a single instance to support hundreds of clients, but it also requires care when accessing instance or class-level variables. Because all threads see the same instance and class variables, access to these variables should be avoided or synchronized using synchronized methods or blocks.

When a JSE is single-threaded, the servlet container normally maintains a pool of JAX-RPC servlets, and therefore JSE instances, then plucks them from the pool to handle client requests. Each simultaneous client request accesses a different JSE instance, so you never have multiple threads accessing the same JSE instance at the same time. Once a client request ends, the JSE instance is returned to the pool so that it can be used to handle another request. You don't have to worry about instance variables; they can be accessed freely without using synchronization. You should avoid creating static variables, on the other hand, or synchronize access to them, just as in the multi-threaded model. The single-threaded programming model has turned out to work poorly in practice because it degrades performance and introduces resource-management problems the multi-threaded model doesn't suffer from. For the most part, servlet experts recommend that you do not use the single-threaded programming model.

The single-threaded model was deprecated as of Java Servlets 2.4, which is the specification supported by J2EE 1.4. In the future the single-threaded model will be phased out of the specification, and only the multi-threaded model will be supported. You can still use the single-threaded model—that's why it's covered in this book—but it's discouraged by the specification.

Which threading model a JAX-RPC servlet and its JSE use depends on whether the JAX-RPC servlet implements the javax.Servlet.SingleThreadModel interface. A JSE that implements the SingleThreadModel interface can be accessed by only one thread at a time—the assumption being that a pool of instances will be used to service requests.

Currently there is no vendor-agnostic way to specify whether a JSE should use a single- or multi-threaded programming model. Your vendor may provide some method for specifying the threading model, or it may support only one threading model for JSEs. Consult your vendor's documentation for details.

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows