Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

What is a thread-safe class?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to Mughal, a thread-safe class is the one which will not
compromise to its integrity due to concurrent call.
Example: String, StringBuffer.
Can somebody explain the whole thing clearly?
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi wahid
if u have a some data in u r class and have defined some methods to modify that and read that or use that.
now if u create a number of threads to manipulate the data and also some threads to read or use the data.
now consider the case when the threads which change the data has changed one instance variable and didnt get time to change other variable (which is important for the object ) and another thread gets time then the data is inconsistent then that thread which got the time will modify or use the data which is inconsistent and this will corrupt the data
so in java we synchronize such methods so that only one thread can access the data and others has to wait for the thread which is accessing the data to wait till the thread finishes it work so that the data wont be corrupt.
hope this clear u r doubt if still in doubt refer the books most books have given a good explanation for this
 
Ranch Hand
Posts: 144
Redhat Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Many methods in Swing are not Thread safe.
Does this mean that we cannot add syncronized in all the methods.but only selective ones.
Please clarify?
______________________
"Logic - is the Art of Non contradictory existence".
Ayn Rand ("Atlas Shrugged"#943)
 
Wahid Sadik
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mathew,
I got the whole idea. Thanx a lot.
Does your explantion mean that both String & StringBuffer have built-in mechnism i.e. sychonized methods so that we don't mess up with them?
 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Wahid Sadik:
Hi Mathew,
I got the whole idea. Thanx a lot.
Does your explantion mean that both String & StringBuffer have built-in mechnism i.e. sychonized methods so that we don't mess up with them?


Hi wahid
i dont whether String and String buffer classes are thread safebut i dont see any reason for String to be thread safe since String is immutable.
 
Wahid Sadik
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sure that both String & StringBuffer are thread-safe.
I've quoted it from Mughal.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Strings are thread safe, because, as Cherry pointed out, they are immutable ... nothing can change them
StringBuffer is also thread safe; following is from the API


String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.


Hope that helps.
Jane
 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jane,
Why should Stringd be thread safe since we are not providing any methods to change them.
Cherry
 
Wahid Sadik
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mathew,
Both String and StringBuffer are final class. So there is no way we can any more methods to them.
Thanx to Jane, you cleared my confusion.
As a summary, classes that are thread-safe, must implement synchronized methods, at least those tend to modify the object
A new question
How can I make immutable class?
I think, while returning a reference from any methods of the class, we will return a reference to a clone of the orginal.
Comments plz.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cherry, Wahid ...
Cherry, String is thread-safe by default because a String object can't be changed; there's no way to get at a String value to alter it.
Probably saying it's thread-safe is a misnomer ... it's really immutable which implies that it's safe from any changes by threads or otherwise.
Whaid,
To make a class immutable you can declare it to be final; which ensures that it cannot be subclassed. You also need to make all it's fields private to ensure they can only be accessed by the class itself.
This is the way in which the String class is declared.
To take a look at the actual source code for the <code>java.lang.String</code> class; go to your JDK directory and enter:

This will create the String.java file in a <code>src/java/lang</code> directory.
Hope that helps.
Jane

[This message has been edited by Jane Griscti (edited January 07, 2001).]
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic