HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
1. Feel Free to ask and submit anything on and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.

java-multithreading: SingleThreadModel Interface vs Synchronization?

Please explain with real time example which approach is better to enable threadsafety in servlet and jsp.

java x 211
multithreading x 21
Posted On : 2016-03-24 01:24:34.0
profile Mantu Kumar - Mantu Kumar


SingleThreadModel is a marker interface which makes sure that you servlet can handle only one request at a time. It is not used frequently. it has it´s own limitation. Synchronization related to thread safe it assures that two threads can not simultaneously access the same resource.

It is not recommended to synchronize the service method(or methods dispatched to it) because there could effect on performance in case of servlet.

In the Single Threaded Model, the container synchronizes on the servlet for you and ensures that only one thread can access it at a time. So the servlet can safely assume only one thread will execute its code at a time.

The single thread model means that your servlet should not be multi-threaded. If there are two concurrent requests to your servlet, then two instances of your servlet will be created to process these two requests. You can implement the single thread model by implementing the SingleThreadModel interface in your class. This is just a marker interface. It does not have any methods.

The multi threaded model means that your servlet would be multi-threaded and only one instance would be created. Multiple concurrent requests can be served by the same instance but in different threads. You can implement the multi threaded model by not implementing the SingleThreadModel interface in your servlet class that´s it.

Posted On : 2016-03-24 23:16:32
Satisfied : 0 Yes  0 No
profile Rishi Kumar - Rishi Kumar
Reply This Thread

Post Answer
Please Login First to Post Answer: Login login with facebook -