Forums Register Login

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

+Pie Number of slices to send: Send
What exactly do you mean when you say that a Vector is synchronized ??
+Pie Number of slices to send: Send
It means that its methods are synchronized.
+Pie Number of slices to send: Send
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 !! ??
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
Let nothing stop you! Not even this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1514 times.
Similar Threads
Synchronized Static Method
What does it mean for a vector to be synchronize?
remove all occurrencies of carriage return from string
Possible solution if image can't be loaded
Vector vs ArrayList
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:32:29.