This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am not sure what does a "synchronized" data structure mean, for instance, a Vector is a synchronized, does that mean there cannot be concurrent writing to it ? When i check the API, there's no sychronized keyword on any of its method. Thanks.
The synchronized part is in reference to iterating through the collection. If while iterating the Vector has an entry added or removed, the iterator will through a ConcurrentModificationException. You can have multiple threads call get or add on the Vector without issue as long as you are not iterating through it.
From the API:
The Iterators returned by Vector's iterator and listIterator methods are fail-fast: if the Vector is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the Iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. The Enumerations returned by Vector's elements method are not fail-fast.
["Ronmate"]: When i check the API, there's no sychronized keyword on any of its method.
That's because sunchronization is not considered part of the API, so even if a method is synchronized, you won't see that when you look at the javadoc. Synchronizing a method is considered an implementation detail - you could achieve the same effect with a synchronized block inside the code, for example. Or you could synchronize on a different object, typically a privately-held object used only for synchronization.
[David]: The synchronized part is in reference to iterating through the collection.
Well no, most of the methods of Vector are in fact synchronized. (Check the source code and see.) Iteration is a special case because you always need at least two different method calls to do anything with it, and so it's not useful to synchronize the methods individually; rather you must synchronize at a higher lever, using a synchronized block. Actually this principle ends up being true for many of the things you might want to do with a Vector, which is a primary reason I think Vector should be abandoned entirely: its "thread safety" is incomplete and leads people to a false sense of security.