0

I have a threaded application, each thread will probably insert specific item into map, or erase its inserted item from map, but for any other threads, they will just use find or traverse the whole map via iterator.

Again, each thread will only insert or erase one its specific item on map.

In such case, should I add lock before insert or erase to avoid race? then how?

tiplip
  • 329
  • 5
  • 17

1 Answers1

0

Without looking at code, I can only say that you use a ConcurrentHashMap for your needs. You may also want to read this : What's the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?

Community
  • 1
  • 1
happybuddha
  • 1,271
  • 2
  • 20
  • 40