File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Synchronized data structure question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Synchronized data structure question" Watch "Synchronized data structure question" New topic
Author

Synchronized data structure question

Ronnie Ho
Ranch Hand

Joined: Aug 10, 2005
Posts: 47
Hi all,

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.
David C. Meyers
Greenhorn

Joined: May 20, 2004
Posts: 14
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
["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.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronized data structure question
 
Similar Threads
Reading db.db in Data Structure.
Collections Question
Array vs List vs Vector
NX: Thread Safety
How to Synchronize a class??