aspose file tools*
The moose likes Java in General and the fly likes What does it mean for a vector to be synchronize? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "What does it mean for a vector to be synchronize?" Watch "What does it mean for a vector to be synchronize?" New topic
Author

What does it mean for a vector to be synchronize?

Fritz Largosa
Ranch Hand

Joined: Sep 12, 2005
Posts: 70
Does it mean the vector object itself is locked down, when ever a code accesses that object? Or does it mean an object in a vector is locked down, when being access? Or does it mean something else?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40051
    
  28
It's not vector, but Vector.

It means that all the methods are flagged with the synchonized modifier. Whenever anything invokes the method, nothing else can invoke the same method until the first invocation has finished. This means the method calls are "atomic," ie they behave as if they consisted of one step only.

Unfortunately it means there is a performance overhead. Hardly anybody uses Vector any more (look in the ArrayList API documentation for what to do if you need a thread-safe List) if they can avoid it. We all use ArrayList instead.
Fritz Largosa
Ranch Hand

Joined: Sep 12, 2005
Posts: 70
Thanks, well unfortunately I am maintaining someone elses code, so I do not really want to do too many changes such as changing Vectors to ArrayList too soon as I am not yet familiar with the architecture and dependencies.

About what you said, I have some basic understanding of how synchronize methods and statements work, as in no other thread can used the synchronize method or statement until it is release by the first thread which locked it. What I do not understand is, how a Vector is said to be thread safe, and why it is called a synchronize object, without the having to put it inside a synchronize statement or method.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40051
    
  28
If you are maintaining old code, you are quite right not to change things unnecessarily.

I am not sure that the synchronization actually makes Vector thread-safe; maybe somebody else will know. Find the file called src.zip in your Java folder on your PC and unzip it. Then you can find the actual code of Vector and can see how many of its methods are synchronized.
Yelamuri Chandu
Greenhorn

Joined: Jan 08, 2008
Posts: 15
What do you understand synchronize here?

Vector is a thread safe object. if you open Vector source code you will see all the method are taged with synchronize keyword which say the access to that methods are controlled or synchronized. which makes Vector a thread safe. when multiple threads are accesing this Vector object, the object wil be synchronized. The quite opposite datastructure to this is ArrayList, which is not thread safe means not synchronized.


When ever you want to create your own datestructure (similar to vector say XYZ) think what will you do if you want it to be thread safe? The answer is synchronize Your XYZ. how to synchronize simple see the source code of Vector
Fritz Largosa
Ranch Hand

Joined: Sep 12, 2005
Posts: 70
Okay, got it. My understanding is that a thread locks down all synchronize methods anytime it executes one of them, once it is done it releases all synchronize methods making all synchronize methods available to the next waiting thread.

My understanding of the ArrayList is that it is not safe for concurrent access since it can be read and written to at the same time by multiple threads. Of course a solution would be to synchronize access to it such as putting it inside a synchronize statement or method. Is my understanding about right?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What does it mean for a vector to be synchronize?