• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread-safe

 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When is a class thread-safe? I thought if a class has synchronized methods then it is thread-safe, (I know it is not correct )? Vector class is thread-safe how?
Can anyone explain this?
Vanitha.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi vanitha ... to my understanding synchronization is one of the techniques to make an object thread-safe ... so could u please elaborate on your question
Samith.P.Nambiar
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Samith,
I know the Vector, Hashtable are thread-safe, but I don't find any synchronized method in that class. Then how do we say they are thread-safe?
Thanks,
Vanitha.
 
Samith Nambiar
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vanitha there are a lot of methods that are synchronized in the Vector class .... here are a few of them
1. copyInto
2. trimToSize
3. ensureCapacity
4. setSize
5 indexOf etc etc ....
hope that helps
Samith.P.Nambiar
<pre>
\```/
(o o) harder u try luckier u get
-------oOO--(_)--OOo----------------------------
</pre>
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Samith, thanks for your reply. Here I go again, those methods don't have the synchronized keyword, that's why I am confused.
Vanitha.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what makes you think they aren't synchronized? Try lookin gat the source code. The API won't tell you whether a method is synchronized:

[This message has been edited by Thomas Paul (edited July 20, 2001).]
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tom,
Thanks for your reply. I don't know how to look at the source code.
Could you plese tell me how?
Vanitha.
[This message has been edited by Vanitha Sugumaran (edited July 20, 2001).]
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vanitha Sugumaran:
When is a class thread-safe? I thought if a class has synchronized methods then it is thread-safe, (I know it is not correct )? Vector class is thread-safe how?
Can anyone explain this?

There is more to being thread safe than just synchronizing some methods. Being thread safe means that no matter how many threads interact with an object, the object will maintain internal consistency and will suplly correct and meaningful data to each thread it interacts with. No thread will be able to corrupt the results of another thread by changing data and making it inconsistent.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the src.jar file which is in your installation of the jdk.
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again Tom,I got the meaning for thread-safe. how to view the source code for an API?
Vanitha.
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tom.
Vanitha.
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
One of the way to make the classes Thread safe is to use local classes.You can define an inner class which holds all the variables, which you think may have problems with syncronization.
Then define/instantiate this class in a method.Since this is a local variable it will not be shared bt different threads.I have used this very often, and found that it gives better performance than using the sunchronized keyoword.
Hope this helps,
Sandeep
SCJP2, OCSD(JDeveloper), OCED(Oracle Internet Platform)
 
reply
    Bookmark Topic Watch Topic
  • New Topic