• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Synchronization of ArrayList and HashMap  RSS feed

 
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 .
 
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
 
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 ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!