wood burning stoves 2.0*
The moose likes Servlets and the fly likes servlet Thread-Safe need explination Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet Thread-Safe need explination" Watch "servlet Thread-Safe need explination" New topic
Author

servlet Thread-Safe need explination

Joe Daly
Ranch Hand

Joined: Dec 18, 2012
Posts: 45
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

Joined: Jan 10, 2002
Posts: 61315
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Guruprasath Manohar
Greenhorn

Joined: Dec 24, 2012
Posts: 1
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
Marshal

Joined: Mar 22, 2005
Posts: 42047
    
  64
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.


Ping & DNS - my free Android networking tools app
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2384
    
  28

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

Joined: Dec 18, 2012
Posts: 45
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

Joined: Jan 10, 2002
Posts: 61315
    
  66

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

Joined: Dec 18, 2012
Posts: 45
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

Joined: Jan 10, 2002
Posts: 61315
    
  66

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

Joined: Dec 18, 2012
Posts: 45
ok thank you very much
 
GeeCON Prague 2014
 
subject: servlet Thread-Safe need explination