Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Gagan Sabharwal wrote:Thanks Jelly for summing it up!
I actually read someone's blog just a few days back, lost that net link.
You have mentioned about storing an object's reference in a static field, if we have say a public static field, will it be thread safe if the object to which this public static field references has even one instance field as non final ?
Will the above line of code be thread safe?I feel it is not, even if its the first thing to happen in class during the time of class loading.
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar wrote:if you want to achieve a thread safety then you have to implement a high level of synchroniztion in you class.
Shanky Sohar wrote:which can be achieved by making all the methods of your class as synchronized and variables as volatile.
SCJP6.0,My blog Ranchers from Delhi
No, there are other techniques for achieving thread safety without any synchronization. Using immutable objects is one; using java.util.concurrent is another.
If you make all the methods synchronized, there's generally no point to making the variables volatile; it's redundant at that point. Further, making all methods synchronized does not guarantee thread-safety - careful analysis is still required.
Also, none of these comments has anything to do with the topic of this thread, other than being about thread safety.
SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar wrote:i think in thread nothing is guaranteed even how much analysis we do..we can try our best but even sometimes JVM reacts in a very different way