• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Synchronized data structure question  RSS feed

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
["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.
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!