0

Previous code is like; to avoid ConcurrentModificationException on a Vector; where ever iteration is required; it was performing inside synchronized block on that Vector. So It is hitting very poor performance by making multiple threads into BLOCKED state to acquire lock on that Vector at different APIs.

I have decided to replace Vector to Collections.newSetFromMap(new ConcurrentHashMap<psConference,Boolean>()); in my project.

So after changing Vector into Concurrent collection; i have removed all SYNCH blocks.

But the problem here is some of my code is performing clone() on that Vector.

  1. How to do the same on here since i have only Set interface ?
  2. Vector clone() is Deep cloning or Shallow cloning ?
  3. Also pls tell me the significance of Boolean at ConcurrentHashMap<psConference,Boolean>
Kanagavelu Sugumar
  • 18,766
  • 20
  • 94
  • 101

3 Answers3

1

But the problem here is some of my code is performing clone() on that Vector.

How to do the same on here since i have only Set interface ?

You are working with a Set now, not a Vector. Your Set is backed by a ConcurrentHashMap, thus is safe to iterate concurrently. Rather then cloning i would suggest you to use a copy constructor.

But be aware (from the javadocs):

However, iterators are designed to be used by only one thread at a time.

That being said, you could also use a CopyOnWriteArrayList, but you have to be careful there, because writes are expensive and the Iterator does not support element changing operations.

Vector clone() is Deep cloning or Shallow cloning?

Clone makes a copy of the references, thus is shallow.

Also pls tell me the significance of Boolean at ConcurrentHashMap<psConference,Boolean>

The Boolean value is just a placeholder since you are using a Map as a Set. If you look at the source of the Collection class you will see that Boolean.TRUE is always used when adding elements. The actual used container for the Set is the Map#keySet(). So the Boolean parameter does actually nothing here, just a placeholder.

Ortwin Angermeier
  • 5,957
  • 2
  • 34
  • 34
  • Thank you very very much!!!! I didn't know that the point "iterators are designed to be used by only one thread at a time". I thought multiple iterators are available for multiple threads at the same time. My bad!!!! – Kanagavelu Sugumar Oct 05 '13 at 04:53
  • Dear @ortang; Could you please also answer my relative question at http://codereview.stackexchange.com/questions/32280/sychronized-block-over-concurrent-collections – Kanagavelu Sugumar Oct 05 '13 at 13:01
0

Personally, I would prefer avoiding concurrency problems as much as possible. Could you maybe send an example of code which throws ConcurrentModificationException? There is maybe a way to re-design algorithms to avoid them.

Also, I would rather use ArrayList to replace Vector.

To answer your point (2), based one this explanation, I would say Vector clone() is probably a shallow clone. By the way, they're also saying that it's usually better to avoid using clone() method.

Community
  • 1
  • 1
ben
  • 1
  • 1
  • Thanks! ben; However SYNCHRONIZED collection has to be replaced with another SYNCH collection to ensure thread safety. It is kid of using ConcurrentHashMap instead of Hashtable for better performance on multi-threaded environment. – Kanagavelu Sugumar Oct 04 '13 at 12:51
0

Keep in mind that the Concurrent versions of the collections will not automagically make concurrent bugs go away.

Still, in your case your best bet would be to create concrete wrapper classes that implement the collection interfaces you expect, delegate all of the necessary methods into the wrapped collection, but know the data types used and know how to create copies of themselves.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40