Hashtable和Collections.synchronizedMap之间的区别(HashMap) [英] Difference between Hashtable and Collections.synchronizedMap(HashMap)

查看:270
本文介绍了Hashtable和Collections.synchronizedMap之间的区别(HashMap)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知, java.util .Hashtable 同步 java.util.Map 界面,而 Collections.synchronizedMap(hash_map) 返回包含同步方法的包装器对象到实际的 hash_map (如果我错了,请更正我)。

As far as I know, java.util.Hashtable synchronizes each and every method in the java.util.Map interface, while Collections.synchronizedMap(hash_map) returns a wrapper object containing synchronized methods delegating calls to the actual hash_map (correct me if I am wrong).

我有两个问题: / strong>

I have two questions :


  1. 它对同步每一个方法和拥有一个包装类有什么区别?

  1. What difference does it make to synchronize each and every method and to have a wrapper class? What are the scenarios to choose one over the other?

当我们执行?这将等于使用正则 java.util.Hashtable


推荐答案

这是我从一些(希望正确的)研究得到的答案:

Here are the answers I've gotten from a bit of (hopefully correct) research:


  1. 两者提供相同的同步度。

  1. Both provide the same degree of synchronization. If you were to wrap HashTable through Collections.synchronized you would have the same degree, but with another redundant layer, of synchronization.

HashTable和HashTable之间的主要区别在于,如果你要通过Collections.synchronized包装HashTable, Collections.synchronizedMap(HashMap)在API级别存在更多。因为HashTable是Java的旧代码的一部分,你会看到HashTable API被增强以实现Map接口,成为Java集合框架的一部分。这意味着,如果您要通过Collections.synchronizedMap()包装HashTable,则包装的HashTable的API将仅限于Map API。因此,如果HashTable的API包含在行为的定义中,则显然会改变/限制。

The main difference between HashTable and Collections.synchronizedMap(HashMap) exist more at the API level. Because HashTable is part of Java's legacy code, you'll see that the HashTable API is enhanced to implement the Map interface, to become part of Java's collections framework. This means that if you were to wrap HashTable through Collections.synchronizedMap(), the API of the wrapped HashTable would become limited to the Map API. So if the API of HashTable is encompassed in your definition of behavior, then it is obviously altered/limited.

这篇关于Hashtable和Collections.synchronizedMap之间的区别(HashMap)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆