• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Synchronization of ArrayList and HashMap

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ..very much
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic