The Artima Developer Community
Sponsored Link

Java Answers Forum
Why iterator() method is required to explicitly synchronize.

1 reply on 1 page. Most recent reply: Jun 15, 2005 8:00 AM by Shashank D. Jha

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Nihar Ranjan Pattanaik

Posts: 10
Nickname: nihar
Registered: May, 2002

Why iterator() method is required to explicitly synchronize. Posted: Jun 13, 2005 5:54 AM
Reply to this message Reply
Advertisement
Why iterator() method is required to explicitly synchronize for a collection returned by calling Collections.synchronizedCollection(Collection c).Why this method is not implemented same way as others like add(),remove() etc.


Shashank D. Jha

Posts: 68
Nickname: shashankd
Registered: May, 2004

Re: Why iterator() method is required to explicitly synchronize. Posted: Jun 15, 2005 8:00 AM
Reply to this message Reply
> Why iterator() method is required to explicitly
> synchronize for a collection returned by calling
> Collections.synchronizedCollection(Collection c).Why this
> method is not implemented same way as others like
> add(),remove() etc.

Unlike add or remove operations called on collection, iterator doesnt modifies its state.

So, in scenarios wherein multiple clients to collection, wants to just iterate over collection to find out the contents, synchronizing the access may be just an overhead.

While add , remove requires to be synchronized so that state is not inconsistent.

While if user wants to modify the state of the collection, and wants to maintain the consistency it needs to properly take care of state update.

Flat View: This topic has 1 reply on 1 page
Topic: java for beginners???? Previous Topic   Next Topic Topic: Street Light Sensors.....

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use