wood burning stoves 2.0*
The moose likes Servlets and the fly likes Thread safety question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread safety question" Watch "Thread safety question" New topic
Author

Thread safety question

jason williams
Greenhorn

Joined: Nov 17, 2004
Posts: 14
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

Joined: Feb 08, 2009
Posts: 597

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....


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
jason williams
Greenhorn

Joined: Nov 17, 2004
Posts: 14
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

Joined: Apr 20, 2010
Posts: 2
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.


SCJP 1.6. SCWCD 5
jason williams
Greenhorn

Joined: Nov 17, 2004
Posts: 14
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

Joined: Jan 14, 2010
Posts: 317

It will be thread safe in that case.

One more information: variables can not be marked synchronized.
Marc Cracco
Ranch Hand

Joined: Mar 09, 2010
Posts: 79
    
    1
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?


SCJP 6 [ My stuff ]
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
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

Joined: Mar 09, 2010
Posts: 79
    
    1
Might sound like a beginner question but why shouldn't you access a static variable from a local synchronized method?
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
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
 
subject: Thread safety question
 
Similar Threads
LinkHashMap problem!
Duplicating a generic inner class to a singleton
non-static reference in singleton
HashMap with unique key & unique values
about map (HashMap to be specific)