• 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

How to make ArraList and HashMap methods synchronized

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
We all know that Vector and Hashtable are synchronized while their counter parts ArrayList and HashMap are not.
Is there a way to synchronize their methods?
If so How?
I have been asked the same question by many interviewers.
First of all what is the need when you have the synchronized methods in Vector and ArrayList Classes.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use Collections.synchronizedCollection()
 
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
But bear in mind that there are relatively few situations where just synchronising all access to a particular Collection is sufficient to achieve proper thread-safety. Usually, you need think about it harder and work out exactly what synchronisation is required. That's one of the reasons Vector and Hashtable are now discouraged, because their synchronisation is rarely actually useful and always expensive.
 
Amit A. Patil
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does it matter? If you have multiple threads modifying a Collection you will always have to synchronize it.?(Multiple writers)
Cant think of any operation that can be unsynchronized...or am i wrong?

hmmm...One Writer ...multiple Reader..still have to synchronize right?
[ September 06, 2006: Message edited by: Amit A. Patil ]
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amit A. Patil:
How does it matter? If you have multiple threads modifying a Collection you will always have to synchronize it.?(Multiple writers)
Cant think of any operation that can be unsynchronized...or am i wrong?

hmmm...One Writer ...multiple Reader..still have to synchronize right?



Amit,

Peter is not saying that certain operations don't have to be synchronized -- I believe he is pointing out that just synchronization at the collection level is not enough.

For example, I have to iterate through the collection, in a thread safe manner. In this case, only getting the iterator is synchronized, which is not good enough. I have to synchronize at a higher scope -- getting the iterator, iterating through the values to calculate a result, and possibly placing the result back.

You have to look at what you are doing and synchronize accordingly -- not just synchronize a collection because it is used by many threads.

Henry
 
Venkat Ramayanam
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So We are not only making it completely thread safe but avoiding the performance hit at the same time by just synchronizing the necessary methods as against using a synchronized collection where its use is less.

Originally posted by Henry Wong:


Amit,

Peter is not saying that certain operations don't have to be synchronized -- I believe he is pointing out that just synchronization at the collection level is not enough.

For example, I have to iterate through the collection, in a thread safe manner. In this case, only getting the iterator is synchronized, which is not good enough. I have to synchronize at a higher scope -- getting the iterator, iterating through the values to calculate a result, and possibly placing the result back.

You have to look at what you are doing and synchronize accordingly -- not just synchronize a collection because it is used by many threads.

Henry

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi this is phalgun and here is my first reply........

i dont know how we make arraylist and hashmar synchronized but we get synchrnoized arraylist and hashmap, using "Collections" class which super class of all collections.in command prompt type "javap java.util.Collections"you will get the documentation.ok byeeeeeeeeeeeee
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
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