• Post Reply Bookmark Topic Watch Topic
  • New Topic

about java.util.Collections  RSS feed

 
song bo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read the java API doc about the java.util.Collections.here is a sample code:

"c" has been synchronized by the synchronization wrapper.but when iterator ,why synchronized it again by manner?I can't understand,what is the function of the "Collections.synchronizedCollection"?Need I synchronize the "add()"、"remove" methods?please help me.thanks you !
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By holding the lock on a synchronized collection while iterating over the collection, you can prevent any objects from being added or removed from the collection -- since all methods are synchronized, none of them can be called by any other thread. Get it?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the implementation that Collections.synchronizedCollection() uses does not synchronize on the required method iterator() defined by the Collection interface. The reason for this is that there is no way to guarantee synchronization if more than one iterator is active. Even if the sychronized Collection did synchronize the iterator() method, once you have the iterator there is no longer any way to protect the data without synchronizing it yourself.
 
song bo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Michael Morris and Ernest Friedman-Hill.so,the iterator() return a iterator that does not synchronized.I must synchronize it by myself.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yes and no. You're missing a subtle point, I think.
If you do something like


with an unsynchronized collection, it's pretty much pointless, because the synchronization won't prevent any of the collection's other methods from being called from another thread. It's only because the collection is synchronized that doing this makes sense in the first place; you can only protect a synchronized collection using synchronization -- i.e., synchronization has no effect on an unsynchronized collection.
See what I mean?
 
song bo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't understand what you mean.especially the last sentence.

synchronization has no effect on an unsynchronized collection.

Generally,I use synchronized(c) to lock a collection.I think it's enough to protect this collection,prevent any methods called from the other threads.like this: "synchronized(c) { do any operations on c ... }".But after studying the java.util.Collections,SUN tell me that I can use Collections.synchronizedCollection() to get a thread-safe collection.so I can not need to manually synchronize it's method except iterator().isn't it?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!