简体   繁体   English

同步块中的 HashMap 与 Collections.synchronizedMap() 有什么区别。

[英]What is difference between HashMap in synchronized block vs Collections.synchronizedMap().

What is difference between HashMap in synchronized block vs Collections.synchronizedMap().同步块中的 HashMap 与 Collections.synchronizedMap() 有什么区别。

HashMap<String,String> hm = new HashMap<String,String>();
hm.put("key1","value1");
hm.put("key2","value2");
synchronized(hm)
{
   // Thread safe operation
}

Map<String, String> synchronizedMap = Collections.synchronizedMap(hm);
// Use synchronizedMap for Thread safe concurrent operation 

Which is better out of these two?这两个哪个更好?

Using the synchronizedMap method is more convenient and safer in that you know all accesses to the map will be protected (as long as you don't bypass it by calling methods on hm directly).使用 synchronizedMap 方法更方便、更安全,因为您知道对映射的所有访问都将受到保护(只要您不通过直接调用 hm 上的方法绕过它)。

But using the synchronized block gives you the ability to control the granularity of locking, by holding the lock over multiple statements, which the synchronizedMap option doesn't allow for.但是使用 synchronized 块使您能够通过将锁保持在多个语句上来控制锁定的粒度,这是 synchronizedMap 选项所不允许的。

So if you need multiple statements to be executed without being interleaved with calls from other threads, you would have to choose the synchronized blocks (or else switch to something like ConcurrentHashMap if you're looking for something like putIfAbsent or similar functionality).因此,如果您需要在不与来自其他线程的调用交错的情况下执行多个语句,则必须选择同步块(或者,如果您正在寻找类似 putIfAbsent 或类似功能的内容,则切换到类似 ConcurrentHashMap 的内容)。 If you don't need that, the synchronizedMap is easier.如果你不需要,synchronizedMap 更容易。

They're the same.他们是一样的。 synchronizedMap() is far easier than handling the syncing yourself, though. synchronizedMap()比自己处理同步要容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Collections.synchronizedMap()和synced之间的区别 - Difference between Collections.synchronizedMap() and synchronized Hashtable和Collections.synchronizedMap(HashMap)之间的区别 - Difference between Hashtable and Collections.synchronizedMap(HashMap) Java同步块与并发HashMap与Collections.synchronizedMap - Java synchronized block vs concurrentHashMap vs Collections.synchronizedMap Java synchronized块与Collections.synchronizedMap - Java synchronized block vs. Collections.synchronizedMap ConcurrentHashMap 和 Collections.synchronizedMap(Map) 有什么区别? - What's the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)? Normal Map和collections.synchronizedmap之间的区别 - Difference between Normal Map and collections.synchronizedmap 在性能方面,ConcurrentHashMap和Collections.synchronizedMap(Map)之间有什么区别? - What's the difference between ConcurrentHashMap and Collections.synchronizedMap(Map) in term of performance? 如果Collections.synchronizedMap在迭代时同步,将是线程安全的吗? - Will Collections.synchronizedMap be thread safe if synchronized while iteration? 如何将Collections.synchronizedMap(new HashMap())转换为Hash Map - How to convert Collections.synchronizedMap(new HashMap()) to Hash Map 在Collections.synchronizedmap上同步 - synchronizing on Collections.synchronizedmap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM