• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Safe Instance Variables

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I make the instance variables in my Servlet static, will that essentially make them thread safe?

Thanks,

Drew
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64958
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Final variables are thread safe.
They can't be changed.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64958
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64958
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic