• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What exactly do you mean when you say that a Vector is synchronized ??

 
Sandeep Kumar S Jakkaraju
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you mean when you say that a Vector is synchronized ??
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that its methods are synchronized.
 
Sandeep Kumar S Jakkaraju
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats great !!....

So it means it can be used as a Shared Object among different threads ...
I am I right ...

What If I want to make some Object ...synchronized ..
For Example :-
MyObject o = new MyObject();
synchronized(o);

will make it synchronized !! ??
 
Jeremy Botha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's probably a better idea to use Collections.synchronizedList() instead of Vector.

Vector is an old class, probably full of WTFery.
;
Sandeep: the code you posted will not compile. If you wanted to make o synchronized you'd either have to access it within a synchronized block, or extend o and implement synchronized methods inside the extending class.

Jeremy
[ October 19, 2006: Message edited by: Jeremy Botha ]
 
Srikanth Raghavan
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very few classes in the JDK are designed keeping thread safety in mind. One of those classes is java.util.Vector. And they are well documented saying that they are thread safe. On the other hand as most of us know the Swing components are not thread safe.

That means, even if you access a Vector object from multiple threads, your data integrity will not be spoiled, but the trade off is speed. In most of the cases the speed will not be a big issue as we will not be coding for a killer search algorithm. But if we are coding for speed and you know for sure that multiple threads will not be allowed to access, then it's better to look at java.util.ArrayList

Hope this helps,
Srikanth
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Srikanth Raghavan:
That means, even if you access a Vector object from multiple threads, your data integrity will not be spoiled


That's not fully true - even though Vector is threadsafe, code that uses a Vector isn't automatically. Often code needs to call several methods on a List in sequence, and when those calls aren't synchronized together, the code might not be thread safe at all.
 
Ashkrit Sharma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is one more usecase when Vector is not thread safe.
You can't add or remove element to vector concurrently,but if you have reference of elements of vector then you can very well modifiy state of those elements. So you need to take care.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic