Vectors contain synchronized methods, and ArrayLists do not. These synchronized methods help prevent data corruption when the data is accessed and modified by >1 thread.
Can someone explain this in more detail for me? What does it mean that Vectors contain synchronized methods? Do the methods contain internal locks that control the locking and unlocking of the data for multiple thread access? Can someone provide some examples of where using a vector (vs. ArrayList) could prevent data corruption and shed some more light on the issue or Data Structures and synchronization issues?
Thanks for your time and help .
The methods have been marked as
synchronized.No, despite the keyword being on the method, it is the object which is locked, not the method. There is no way to lock a method as such.
There are many examples. The simplest is incrementing a number. Say you have two threads incrementing a number (e.g. the size of the collection) Without synchronization you can have.
however as threads can perform actions in any order (as this is the whole point of threads) you can also have
So even though two threads have incremented the counter, it is incorrect because their actions where not co-ordinated. This is what synchronization does for you.