• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

What do you mean my thread safe ??

 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!

 
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Ernest Friedman-Hill very good explanation even non-java programmer also can easily understand that. Thank you.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic