aspose file tools*
The moose likes Threads and Synchronization and the fly likes What do you mean my thread safe ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "What do you mean my thread safe ??" Watch "What do you mean my thread safe ??" New topic
Author

What do you mean my thread safe ??

Saurabh Agrawal
Ranch Hand

Joined: Oct 07, 2003
Posts: 244
Hi friends ,
Can anyone tell me what is the meaning of thread safe precisely ??
Also tell me is java affected my viruses and if not why ??
Thanks,
Saurabh


Success is not doing extraordinary things but doing ordinary things extraordinarily well.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Saurabh Agrawal:

Can anyone tell me what is the meaning of thread safe precisely ??

Imagine a Person object, with many member variables; among these are three variables that represent the street, city, and postal code where they live.
You are writing a function which prints mailing address labels.
Now, imagine that one person is moving to another city, which requires changing those three variables in one Person objects. Let's say that person has just filled in a form on the Web which lets him change his address. The Web application has to set those three variables, one after another. The first one it sets is the new street address. The city and postal code are next.
Now imagine, that just at the instant that the street address is changed, your function (running in another thread) starts to print an address label for that person. The address label includes the person's name, their new street adress, but their old city and postal code. Any mail with that label on it will never be delivered correctly!
The problem is that the address-setting operation is not thread-safe. During the short time that the Web application is changing those three fields, the Person object is in an invalid state. If another thread looks at that Person's address variables during that time, it will get incorrect information.
To make this operation thread safe, you must write the methods in such a way that the address-setting process cannot be interrupted by any other thread; while the three variables are being set, no other thread can read any of them. This can be accomplished in Java using synchronized methods.

Also tell me is java affected my viruses and if not why ??

Some Java programs could indeed be vulnerable to "viruses" (actually most things we call "viruses" these days really aren't viruses in the classic sense of the term, but "Trojans") but it's much harder to write Java viruses than other kinds, and much easier to defend against them. In some contexts (applets in a browser, for example) more or less impenetrable defenses are already in place without you yourself doing anything at all.


[Jess in Action][AskingGoodQuestions]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
A class is thread safe if its instances can have their static/instance methods called simultaneously by several threads without compromising the invariants of the class. Synchronization serializes the accesses to these methods.
The invariants of a class are assertions that are though to be true. With a similar meaning we can say the object is in a consistent state if all its fields have a proper value.
For instance, a class modelling a semaphore could have an invariant such as not allowing its color field to pass directly from green to red without an intermediate yellow. If that object has several threads calling the setter method for the color field without synchronization another thread reading such value could observe a sequence that broke the invariant.
Maybe a clearer invariant for this class is not allowing any color apart from red, green and yellow.
Joshua Bloch wrote in Effective Java.

...misconception that thread safety is an all-or-nothing property. In fact there are many levels of thread safety that a class can support:
* immutable. Instances of this class appear constant to their clients. No external synchronization is neccesary.
* thread safe. Instances of this class are mutable, but all methods contain sufficient internal synchronization that instances maybe used concurrently withou the need for external synchronization.
* conditionally thread-safe. Like thread-safe, except that the class contains methods that must be invoked in a sequence without inteference from other threads. To eliminate the possibility of interference the client must obtain an appropiate lock for the duration of the sequence. Examples includes Hashtables and Vectors, whose iterators require external synchronization.
* thread compatible. Instances of this class can safely be used concurrently by surrounding each method invocation (and in some cases, each sequence of methods invocations) by external synchronization. Examples include ArrayList and HashMap.
* thread-hostile. These classes are not thread-safe even surrounding each method invocation by external synchronization. Tipically these methods modify static data that affect others threads. Luckily they are very few. One of them System.runFinalizersOnExit has been deprecated.


SCJP2. Please Indent your code using UBB Code
Gilbert Prashant Singh
Greenhorn

Joined: Nov 02, 2011
Posts: 5
Ernest Friedman-Hill wrote:
Originally posted by Saurabh Agrawal:

Can anyone tell me what is the meaning of thread safe precisely ??

Imagine a Person object, with many member variables; among these are three variables that represent the street, city, and postal code where they live.
You are writing a function which prints mailing address labels.
Now, imagine that one person is moving to another city, which requires changing those three variables in one Person objects. Let's say that person has just filled in a form on the Web which lets him change his address. The Web application has to set those three variables, one after another. The first one it sets is the new street address. The city and postal code are next.
Now imagine, that just at the instant that the street address is changed, your function (running in another thread) starts to print an address label for that person. The address label includes the person's name, their new street adress, but their old city and postal code. Any mail with that label on it will never be delivered correctly!
The problem is that the address-setting operation is not thread-safe. During the short time that the Web application is changing those three fields, the Person object is in an invalid state. If another thread looks at that Person's address variables during that time, it will get incorrect information.
To make this operation thread safe, you must write the methods in such a way that the address-setting process cannot be interrupted by any other thread; while the three variables are being set, no other thread can read any of them. This can be accomplished in Java using synchronized methods.



This is a far better explanation for a beginner....and explains concisely. Loved the explanation! Thanks!
Mohana Rao Sv
Ranch Hand

Joined: Aug 01, 2007
Posts: 485

@Ernest Friedman-Hill very good explanation even non-java programmer also can easily understand that. Thank you.


ocjp 6 — Feeding a person with food is a great thing in this world. Feeding the same person by transferring the knowledge is far more better thing. The reason is the amount of satisfaction which we get through food is of only one minute or two. But the satisfaction which we can get through the knowledge is of life long.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What do you mean my thread safe ??