Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet Thread-Safe need explination

 
Joe Daly
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SingleThreadModel is long deprecated, so it's irrelevant.

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.
 
Guruprasath Manohar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Daly
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so how would i have say for example a static variable counting how many times a request method e.g. doGet() is called?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Daly
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Session and application scopes.

As to the latter part of your question, it has nothing to do with thread safety so it'd best be asked separately. In fact, SearchFirst; it's an often-asked question.
 
Joe Daly
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thank you very much
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic