aspose file tools*
The moose likes Threads and Synchronization and the fly likes Shared Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Shared Variables" Watch "Shared Variables" New topic
Author

Shared Variables

Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
What are the ways of making a shared variable? Is there any other way apart from making it static?


SCJP 1.4, SCWCD 1.4, SCBCD 1.5
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
I am getting a bit confused on what kind of variables are shared.

1) If we have a class x, which extends thread or implements runnable.

are the instance variables in class x thread safe
Eg
Class x implements Runnable{
String x = null; <- is this thread safe
}
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Relatively few things are inherently thread-safe.

Local variables in a method are thread-safe. There is no way another thread can change them. But beware of local variables that are object references. The object reference is thread-safe, but the object that it points to may be shared between threads, depending on who else has got a reference to the object.

ThreadLocal objects and many classes in the concurrency package (Java 5+) are thread-safe. That's rather their raison d'etre.

Some other API objects are thread-safe, to some extent. However, as in the case of Vector and Hashtable, this may only mean that their internal state cannot get messed-up by being shared between threads. If the "thread-safe" object is part of a larger structure, then that structure is not thread-safe unless additional precautions are taken.

Being static or non-static has little bearing on whether a field is thread-safe. Non-static fields can certainly be shared between threads.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
can you please give me a example of not static variable being thread safe. And secondly are the instance variables in the thread class(the one in my first post) is thread safe?

Thanks
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
I mean example of non static varible sharing?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
How about...

Now I've shared my variable with two other threads. They can easily get into race conditions.

Say MyRunnable did this:

That could print true or false because the other thread might be setting it to 42 in between lines. If MyRunnable is to be thread safe, it must enclose any two calls where it expects consistent state in synchronized blocks. If I put those two lines in a synchronized(sharedArgument) block then I know that won't happen.

Is that making sense so far? Try the Sun Concurrency Tutorial for more good stuff.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
Thanks a ton. Yeah its getting better now....
 
jQuery in Action, 2nd edition
 
subject: Shared Variables