Guruprasath Manohar

Greenhorn
+ Follow
since Dec 24, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Guruprasath Manohar

Hi Joe,

Generally in servlets, there will be a single servlet instance and when a request comes up, a thread will be created and it will start executing using the single instance. Because of this the common data (instance variables, static variables, session attributes, application attributes) can be accessed by two threads at the same time, causing problem.
So what needs to be done is to protect the variables from getting accessed or written at the same time by two different threads.

1. SingleThreadModel was introduced to solve this concurrency issue. In this model, there will be single thread only. So the servlet will service only a single request at a time and once its done it can work on the next request. But this will make the application worst since there can be only one user who can hit the servlet at a time. So it was decided to have several instances for a servlet in a pool and once a request comes up it will be assigned to a instance so that multi-user feature is saved. But even this cant solve the static variables, application and session attributes being accessed by the different instances. So this model has been deprecated now.
2. Synchronization is used to specify a block of code or method which you feel should not be accessed by two threads at a same time. The synchronized keyword is used for this purpose. You need to read a good Java book to understand what is synchronization.
As Bear told, its not necessary to use Synchronization. If you are careful in your design of accessing and writing the various variables and attributes, that will solve the problem.
11 years ago