Success is not doing extraordinary things but doing ordinary things extraordinarily well.
Originally posted by Saurabh Agrawal:
Can anyone tell me what is the meaning of thread safe precisely ??
Also tell me is java affected my viruses and if not why ??
...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
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!
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.
Don't get me started about those stupid light bulbs. |