Hi i am reading the head first book on servlets and jsp,
i just read pages 190-205 and i don't quiet understand much about synchronize, which variables are thread safe, SingleThreadModel, ect..., could anyone please explain these things in great detail, and tell me if they are important??
And synchronize is to be avoided unless absolutely necessary.
What it really comes down to is writing the code such that variables are not shared across threads. About 99% of that is simply accomplished by avoiding instance variables. The remaining 1% is mostly being careful with session-level scoped variables and application-level scoped variables.
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.
Guruprasath Manohar wrote:As Bear told, its not necessary to use Synchronization.
Bear didn't say that, and it wouldn't be correct to say it. Synchronization is absolutely necessary when accessing shared mutable state. Any reasonably complex application will have such state, so it's necessary to use synchronization in some form. You're right that this is a tricky subject, and one should peruse a book such as "Java Therads" or "Java Concurrency in Practice" to learn about the various ways in which that can be achieved.
Most applications have shared mutable state. OTH most applications keep state in database, and the database takes care of the "synchronization". The database makes sure that concurrent sessions cannot modify the same piece of data. So, for the most part, applications don't need to do explicit synchronization. Applications will need to do explicit synchronization if state is stored in resources that do not protect themselves from concurrent modifications; eg; memory, local file system.
Joined: Dec 18, 2012
so how would i have say for example a static variable counting how many times a request method e.g. doGet() is called?
That's pretty useless, but is an example of a value that you want to share across threads. Such values are few and far between.
And, a class variable is not an instance variable. There is rarely if ever a time when sharing an instance variable is appropriate -- if data needs to be shared, there are better ways to accomplish that.
Joined: Dec 18, 2012
Bear Bibeault wrote:if data needs to be shared, there are better ways to accomplish that.
could you explain how, and i am curious what other stuff i should learn after jsp and servlets?? that is used in the industry