0

After these posts Locking value objects of a ConcurrentHashMap

ConcurrentHashMap: how to replace the value of an entry only if the current value is smalle

Can we use Synchronized for each entry instead of ConcurrentHashMap?

And spending so much time, I think I should give up on concurrency (at least for now) and forget entry locking. It is too error-prone. Several times I came up with a solution, but again I found a bug.

So, if I want to scarify concurrency, and jut have a correct program, I think the easiest what is locking the whole hash table. So what is the best way to do that. I have 6 hash tables. One solution is using 6 HashMaps and whenever I want to do something use synchronized block. Is it OK, or I need to use ConcurrenHashMap? Note that I am using synchronized block.

Community
  • 1
  • 1
Mohammad Roohitavaf
  • 439
  • 2
  • 6
  • 15
  • I read somewhere that we you are writing a multithreaded program you should not use HashMap AT ALL. So my question is, even if I use synchronized whenever I what to do anything with that, using HashMap is a problem? I am insisting on using HashMap, because I think it has better performance than ConcurrentHashMap. – Mohammad Roohitavaf Jul 20 '16 at 22:44
  • What purpose does a hashmap with synchronized block solve? as you already added synchronized, performance is gonna be slow anyways. – ajay.patel Jul 20 '16 at 22:47
  • I know. But which one is better? Synchronized + HashMap or Synchronized + ConcurrentHashMap. Note That I need use synchronized anyway, even if I use ConcurrentHashMap. – Mohammad Roohitavaf Jul 20 '16 at 22:51
  • are you sure SynchronizedHashMap doesn't solve your problem? Locks are at object level in this type of map. – ajay.patel Jul 20 '16 at 22:57
  • The problem is some of my hash maps has value with type hash map or list,... When I access get a key, OK the get operation is synchronized or whatever, but it is possible that two thread work on the inner hashmap (the value). – Mohammad Roohitavaf Jul 20 '16 at 23:01
  • 1
    You haven't shared any code regarding the problem you're trying to solve. If you shared some code and specified your requirements, this question could perhaps be answerable. – Mick Mnemonic Jul 20 '16 at 23:41

0 Answers0