Using ConcurrentHashMap

J

James Yong

Hi,

I have google and couldn't find any tutorial/examples on using
ConcurrentHashMap.
Suppose I have the following three scenarios, what should I do to ensure
thread-safety?


if (!map.containsKey(key))
map.put(key, value);


for (int i=0; i<list.size(); i++) {
doSomething(list.get(i));
}


for (Iterator i=list.iterator(); i.hasNext(); ) {
doSomething(i.next());
}



Regards,
James Yong
 
R

Robert Klemme

James Yong said:
Hi,

I have google and couldn't find any tutorial/examples on using
ConcurrentHashMap.
Suppose I have the following three scenarios, what should I do to
ensure thread-safety?


if (!map.containsKey(key))
map.put(key, value);

In this case a normal HashMap is sufficient because you need external
synchronization anyway:

synchronize ( map ) {
if (!map.containsKey(key))
map.put(key, value);
}
for (int i=0; i<list.size(); i++) {
doSomething(list.get(i));
}


for (Iterator i=list.iterator(); i.hasNext(); ) {
doSomething(i.next());
}

These are no maps. Also it depends on what you want to allow. The easiest
again is external synchronization on the complete container for the complete
iteration. Difficult to answer without further info on your requirements.

Regards

robert
 
J

James Yong

Hi Robert,

Robert Klemme said:
In this case a normal HashMap is sufficient because you need external
synchronization anyway:

Thanks for the explanation above ;-)
synchronize ( map ) {
if (!map.containsKey(key))
map.put(key, value);
}


These are no maps. Also it depends on what you want to allow. The easiest
again is external synchronization on the complete container for the complete
iteration. Difficult to answer without further info on your requirements.

Regards

robert

Yeah, you are right that the 2nd and 3rd scenario has nothing to do with
Maps.
I am trying to use Doug Lea's util.concurrent package with Java 1.4. I am
not sure if I need to implement
a different Iterator if I were to use from the package to replace List and
Maps
I took the examples blindly from
http://www-128.ibm.com/developerworks/java/library/j-jtp07233.html


Guess I have to spend some time looking into the source code of the
packages. Thanks

Regards,
James
 
T

Thomas Hawtin

James said:
I have google and couldn't find any tutorial/examples on using
ConcurrentHashMap.
Suppose I have the following three scenarios, what should I do to ensure
thread-safety?


if (!map.containsKey(key))
map.put(key, value);

map.putIfAbsent(key, value);

Unfortunately there isn't a createIfAbsent(K said:
for (int i=0; i<list.size(); i++) {
doSomething(list.get(i));
}
for (Iterator i=list.iterator(); i.hasNext(); ) {
doSomething(i.next());
}

I assume this is CopyOnWriteArrayList.

for (E e : list) {
doSomething(e);
}

The iterator works on a constant version of the list.

Synchronizing to the whole ConcurrentMap/CopyOnWriteArrayList wont help
as the point is to avoid collection-global locks.

Tom Hawtin
 
J

James Yong

Thomas Hawtin said:
map.putIfAbsent(key, value);

Thanks Thomas. I think you have more or less clear some doubts I have ;-)
I assume this is CopyOnWriteArrayList.

for (E e : list) {
doSomething(e);
}

The iterator works on a constant version of the list.

Synchronizing to the whole ConcurrentMap/CopyOnWriteArrayList wont help
as the point is to avoid collection-global locks.

Yes, I agree on that too ;-)

Regards,
James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top