This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Servlets and the fly likes Thread Safe Instance Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread Safe Instance Variables" Watch "Thread Safe Instance Variables" New topic
Author

Thread Safe Instance Variables

Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
If I make the instance variables in my Servlet static, will that essentially make them thread safe?

Thanks,

Drew
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
No.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Since there is only one copy of the servlet created, the only thread-safe variable are automatic variables inside the methods.

What led you to believe that turning instance variables into class variables would somehow make something thread-safe?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
And if you really keen to make your servlet instance variable threadsafe then you have to write the code yourself to make it threadsafe.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Bear Bibeault:
Since there is only one copy of the servlet created, the only thread-safe variable are automatic variables inside the methods.


Or instance scoped final variables.
IOW: variables declared final but not initialized in the declaration.
Once initialized (usually in the servlet's init method) they are frozen like constants. Since they can't be changed, they are thread-safe.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
Originally posted by Bear Bibeault:
Since there is only one copy of the servlet created, the only thread-safe variable are automatic variables inside the methods.

What led you to believe that turning instance variables into class variables would somehow make something thread-safe?


I have this old "Core Servlets" book by Marty Hall. There is a passage regarding the isThreadSafe attribute where he says:
Be careful about using isThreadSafe="false" when your servlet has instance variables (fields) that maintain persistent data. In particular, note that servlet engines are permitted (but not required) to create multiple servlet instances in such a case and thus instance variables are not necessarily unique. You could still use static fields in such a case, however.


I guess he means the static fields would be thread safe only if you implement the SingleThreadModel. Right?

Drew
Nikhil Menon
Ranch Hand

Joined: Nov 22, 2004
Posts: 70
Hi,

Static variables are the least thread safe as other.

Static variables are never thread safe. These variables are at class level, so they are shared between all instances. Hence these variables are not thread safe even if the servlet is implementing the SingleThreadModel interface. That is why they are usually used to store only constant/read-only data.



you can have a look at this link
http://www.whizlabs.com/tutorial/scwcd/j-scwcd-8-4.html

Nikhil
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Drew Lane:


I guess he means the static fields would be thread safe only if you implement the SingleThreadModel. Right?

Drew


Static variables would be un-safe even if you were implementing SingleThreadedModel.

A static variable is one shared by all instances of the class.
Jack Daniel
Ranch Hand

Joined: Jun 15, 2002
Posts: 163
I very well know, that the Static members are not thread safe,..
but in my servlets I always use:

as a class member,

because i do the data source JNDI lookup in init() and get connection in other methods...

This is the only static member I have; and definitely NO instance members.
I know this is not thread safe.. but there is no need for thread safety on this issue..
Your opinion on this?? is it a bad approach??
thanks
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
OK, I tried to get rid of all the instance variables to be safe.

However, I still have some final static instance variables that are initialzed in the class. I don't understand why these might cause a thread safety issue, too? If they are final, why aren't they "set in stone"?

Drew
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Final variables are thread safe.
They can't be changed.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

One could even go as far to say that non-final variables that are set up once during init() and never touched again (read-only from that point on) would be safe -- but that's a slippery slope...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Which is exactly why you should declare them as final.
You can only set them once, then they're locked.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Actually you cannot make them final if they are set in init(), but I never do that anyways. Application context is such a better home for such data.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Bear Bibeault:
Actually you cannot make them final if they are set in init()

Ack!!!, you're right.

I use servlet init params quite a bit and had plans to solidify my servlets by making them final, with deferred assignment taking place in the init method.

If anyone's ever interested in why you can't assign them in init(), this explains it.
http://java.sun.com/docs/books/jls/second_edition/html/defAssign.doc.html

In a nutshell, the compiler has to know that the assignment comes before any other access. Since the compiler doesn't know that the init method will always be called before one of the service methods, it blows up.
Oh well,
Thanks Bear..
[ February 17, 2005: Message edited by: Ben Souther ]
Raja gopal Y
Greenhorn

Joined: Mar 08, 2005
Posts: 15
What i am thinking is, All the variables inside the service()[or doGet / doPost] method are not Threadsafe, all the remaining variables are ThreadSafe.

Because Only the Service method is called many times by different threads on a servlet instance.

Any Comments !!!


CHEERS !!!<br /> <br />RAJAGOPAL Y
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Raja gopal Y:
What i am thinking is, All the variables inside the service()[or doGet / doPost] method are not Threadsafe, all the remaining variables are ThreadSafe.

Because Only the Service method is called many times by different threads on a servlet instance.

Any Comments !!!


No, you've got it backwards. The variables inside the service methods are thread safe because they are accessible only to the thread that owns them.
Instance variables are NOT threadsafe because they are accessible to all threads from within that servlet.
 
jQuery in Action, 2nd edition
 
subject: Thread Safe Instance Variables