What does it mean when we say Hashtable or Vector is synchronized?

后端 未结 4 869
借酒劲吻你
借酒劲吻你 2020-12-30 14:27

The questions says it all, just wondering, in an interview, what would you say when they ask you, \"What does it practically mean by Hashtable or Vectors being synchronized?

4条回答
  •  伪装坚强ぢ
    2020-12-30 14:44

    Only one thread of execution can change the state of the container at any given time.

    If the container was not synchronised, multiple thread of execution could try to change the state of the container at the same time. This would of course most likely end up with the internal state of the container being corrupted, i.e. not what you want.

    The original Java containers, e.g. java.util.Vector and java.util.Hashtable, were all synchronised for safety reasons. But the downside of synchronising access as a default is that use cases where synchronisation is not necessary will suffer the performance penalty of synchronisation. So now Java ships with unsynchronised containers as well, e.g. ArrayList and HashMap.

提交回复
热议问题