-6

In JDK1.8, is it necessary to use ConcurrentHashMap on all occasions that require synchronization?And never use Collections.synchronizedMap (Map)?

xcy
  • 3
  • 1
  • 1
    You use `ConcurrentHashMap` *instead* of synchronization. There is no basis for your supposition. – user207421 Apr 03 '17 at 03:24
  • 1
    You mean instead of `synchronized`, @EJP. `ConcurrentHashMap` is a synchronized structure, it just doesn't use the keyword `synchronized` to achieve synchronization. – Lew Bloch Apr 03 '17 at 03:47
  • No, it isn't necessary to use one idiom or the other. Use whichever one best suits the needs of the implementation. – Lew Bloch Apr 03 '17 at 03:48
  • @LewBloch `ConcurrentHashMap` is *not* `synchronized` – Andreas Apr 03 '17 at 03:49
  • @Andreas It doesn't use the keyword `synchronized`, as I assume you mean by putting the keyword in backticks, but it achieves synchronization without that. https://en.m.wikipedia.org/wiki/Synchronization_(computer_science) – Lew Bloch Apr 03 '17 at 03:53
  • 1
    @LewBloch Huh?!? The article you linked defines synchronization as one of two distinct concepts: synchronization of processes (meeting up), and synchronization of data (replication), neither of which fit the concept of synchronization of a `Map`, which is all about *preventing* concurrent access. This is a Java question, and in Java terminology, "synchronization" means the use of `synchronized`. This question is about the difference between "concurrent" and "synchronized" collections, and even the javadoc uses those terms to be distinct from each other. – Andreas Apr 03 '17 at 04:09
  • @Andreas I agree that it's a terminology issue. There is one context in which it is correct though to refer to it as "synchronized" - use of data in ConcurrentHashMap is "correctly synchronized" in the context of the Java Memory Model specification ([A program is correctly synchronized if and only if all sequentially consistent executions are free of data races.](https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html)) – Erwin Bolwidt Apr 03 '17 at 04:17
  • @ErwinBolwidt Just because "correctly synchronized" requires that "executions are free of data races", does mean that all executions that are free of data races are synchronized. – Andreas Apr 03 '17 at 04:25
  • @Andreas Actually that's not true: in the JMM, if all sequentially consistent executions are free of data races, then the program is "correctly synchronized". Even if it doesn't encouter any use of Java monitors (`synchronized` keyword) during the execution. You're getting hung up on the word synchronized which does not always mean "the *keyword* `synchronized`", which is the point that both Lew and I have been trying to make to you. – Erwin Bolwidt Apr 03 '17 at 05:37
  • 1
    @LewBloch No, I do *not* mean `synchronized`, and no, it is *not* a synchronized structure, it is a *thread-safe* structure, all achieved without any synchronization at all, *or* `synchronized`. – user207421 Apr 03 '17 at 06:07

1 Answers1

0

No, it is not necessary to replace use of Collections.synchronizedMap with ConcurrentHashMap when used by multiple threads.

The Concurrent Collections section in the javadoc for package java.util.concurrent says:

"Synchronized" classes can be useful when you need to prevent all access to a collection via a single lock, at the expense of poorer scalability. In other cases in which multiple threads are expected to access a common collection, "concurrent" versions are normally preferable.

As you can see, both Collections.synchronizedMap and ConcurrentHashMap can have their uses.

See also "What's the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?".

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247