Tuesday, April 17, 2007

How do you make servlet thread-safe?

SingleThreadModel interface once implemented makes a servlet thread safe,though not in complete sense.It ensures that servlet handles only one request at a time. In this case only one thread will be able to execute service() method at a time.Though it affects the performance of the servlet to a great extent. In STM container creates multiple instances of servlet.This interface is deprecated from Servlet API version 2.4.

It does not resolve all thread safety related issues of a servlet, as most of session attributes and static variables can still be shared across multiple threads at the same time.The other ways to handle such situations can be avoid creating instance variables or synchronizing the block of code accessing those resources.

1 comments :

P-H on February 11, 2011 at 9:27 PM said...

Hi,

I really like the format of your blog. Quite useful for people starting their career in Java EE.

I have seen many examples of wrong implementation of Servlet leading to Thread locking and severe problems; exactly like you mentionned:

- synchronize block within Servlet method
- Instance variables; this is one of the worst case as it can lead to intermittent and unexpected runtime behaviour as Threads are sharing the instance variable object


Regards,
P-H
https://javaeesupportpatterns.blogspot.com/

 

Disclaimer
Interview Questions On Java,Java EE Copyright © 2016. Reads: best tracker