• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make ArraList and HashMap methods synchronized  RSS feed

 
Venkat Ramayanam
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.
 
Amit A. Patil
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Collections.synchronizedCollection()
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
dandu phalgun kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!