aspose file tools*
The moose likes Threads and Synchronization and the fly likes What exactly do you mean when you say that a Vector is synchronized ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "What exactly do you mean when you say that a Vector is synchronized ??" Watch "What exactly do you mean when you say that a Vector is synchronized ??" New topic
Author

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

Sandeep Kumar S Jakkaraju
Ranch Hand

Joined: Oct 18, 2006
Posts: 37
What exactly do you mean when you say that a Vector is synchronized ??


I write like Jean Paul Sarte but in Java....
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
It means that its methods are synchronized.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Sandeep Kumar S Jakkaraju
Ranch Hand

Joined: Oct 18, 2006
Posts: 37
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

Joined: Feb 16, 2005
Posts: 125
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 ]

McFinnigan? Never heard of him. Nobody here but us chickens...<br /> <br />SCJP for Java 1.4<br />SCJD for Java 5.0
Srikanth Raghavan
Ranch Hand

Joined: Oct 31, 2005
Posts: 389
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Oct 05, 2006
Posts: 7
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What exactly do you mean when you say that a Vector is synchronized ??