File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shared Variables

 
Nikhil Jain
Ranch Hand
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the ways of making a shared variable? Is there any other way apart from making it static?
 
Nikhil Jain
Ranch Hand
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nikhil Jain
Ranch Hand
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean example of non static varible sharing?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nikhil Jain
Ranch Hand
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton. Yeah its getting better now....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic