• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread safety question

 
jason williams
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I search the forum and on the internet, but seems there is no thread answering my doubt. So I would like to post a new thread to make clear.

I have a servlet, which in turns call another class (e.g. class X{...} ), in which there is a class variable (e.g. static Map map = Collections.synchornizedMap(..)). In servlet, all variables are local (inside doGet/ doPost) and class variable (e.g. static class ...{} ). But in the class X, it has instance method (e.g. doSomething()) where it would operate actions similar to `if put absent' scenario. For instance,



Consider this scenario. the method doSomething() inside class X will be thread safe?

Thanks for help.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nopes does not look thread safe to me. In particular,



One thread could get the value from the map. Context switch could occur at this point. Second thread gets the same value from the map instead of the value stored back by the first thread....
 
jason williams
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

Does that mean even if this class is used as a local variable inside a servlet, it is still not thread safe? That is what me confused because I read some document on the internet saying that a local variable is always thread safe. For instance.




Thanks again for your help.
 
Fairoz Shaikh
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you create the local variable (object) of the class X, every thread will have its own object of class X sharing the same static member of class X (i.e. map). So its not thread safe.
 
jason williams
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fairoz Shaikh wrote:Even if you create the local variable (object) of the class X, every thread will have its own object of class X sharing the same static member of class X (i.e. map). So its not thread safe.


Thanks for the explain. If the variable e.g. map is not declared as static (which would be referenced by all other threads running in the servlet in that case) in class X, and class X in the servlet is defined as local variable (e.g. X x = new X(); in doPost method ), does that mean it is thread safe? (In this case, the map if not declared as static, the result may go wrong; but just want to make clear about the issue of thread safety. )



Thanks again for all your help.
I appreciate it.


 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will be thread safe in that case.

One more information: variables can not be marked synchronized.
 
Marc Cracco
Ranch Hand
Posts: 80
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still new at this and others can correct me if I'm wrong but if you made your
map variable private static and in any of the methods that change it's value
make those methods synchronized then it should be thread safe...

Maybe?
 
amitabh mehra
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To access static variables from methods, those methods should be static. In that case, you need to synchronize on the Class.
 
Marc Cracco
Ranch Hand
Posts: 80
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Might sound like a beginner question but why shouldn't you access a static variable from a local synchronized method?
 
amitabh mehra
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc Cracco wrote:Might sound like a beginner question but why shouldn't you access a static variable from a local synchronized method?


Sorry if my previous post confused you. You are right, static variables can be accessed in local synchronized methods. But the lock should be on the Class again or some other static lock object. That is what i wanted to say in my previous post; but framed the sentence poorly
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic