0%

hashtable-faq

1. HashTable 实现原理

通过在 get, put 方法上加 synchronized 关键字来保证线程安全.

2. HashTable 的 size() 方法中明明只有一条语句"return count", 为什么还要做同步?​

通过 synchronized 修饰方法, 锁住的是当前对象.
同步 size() 方法, 是为了防止在取 HashTable 大小时, 有 put() 或者 remove 方法修改 HashTable 的结构, 导致 count 值不准确.