Posts

Showing posts with the label Servlets

Servlet Concurrency - Java

Image
A Java servlet container / web server is typically multithreaded. That means, that multiple requests to the same servlet may be executed at the same time. Therefore, you need to take concurrency into consideration when you implement your servlet. To make sure that a servlet is thread safe, there are a few basic rules of thumb you must follow: Your servlet service() method should not access any member variables, unless these member variables are thread safe themselves. Your servlet service() should not reassign member variables, as this may affect other threads executing inside the service() method. If you really, really need to reassign a member variable, make sure this is done inside a synchronized block. Rule 1 and 2 also counts for static variables. Local variables are always thread safe. Keep in mind though, that the object a local variable points to, may not be so. If the object was instantiated inside the method, and never escapes, there will be no problem. On the othe...