• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronization of ArrayList and HashMap  RSS feed

 
Monoj Roy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList and Vector are List Implementation but they have a difference that Vector's methods are Synchronized and ArrayList is not .

The same Logic for the HashMap and Hashtable .

Now I want to make ArrayList and HashMap synchronized .Is there any way I can do the same without using Synchronized keyword ? That is I dont want to put all the methods inside a Synchronized block .
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is the Collections.synchronizedList method for this. But be sure to read the javadoc explaining you still have to synchronize when you iterate over the list.
 
Monoj Roy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ..very much
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be careful. Synchronising an individual List or Map is rarely sufficient to make a piece of code thread-safe. It makes the actual List or Map immune to corruption due to multi-threaded access, but there are almost always other concurrency issues to address; these very often mean that coarser-grained synchronisation is needed.

For those rare occasions when synchronising an individual List or Map really is what you need, then Collections.synchronizedList/Map() methods are indeed useful.
[ November 05, 2007: Message edited by: Peter Chase ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!