为什么像ArrayList和HashMap这样的类不同步? [英] Why are classes such as ArrayList and HashMap not synchronized?

查看:134
本文介绍了为什么像ArrayList和HashMap这样的类不同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新一个旧项目,我想知道是否应该删除IDE中标记为过时的一些对象。有问题的对象是Vector和Hashtable。我已经做了一些研究,似乎这些对象的新对手 - ArrayList和HashMap - 基本相同,只是没有同步。



我的问题是,我为什么要做这个开关?难道我不是从我的应用程序中拿走一点安全性来换取少量速度?似乎这不是一个巨大的决定,但我认为我会看到是否值得花时间做出决定。



谢谢!

解决方案


是不是我只是从我的应用程序中拿走一点安全性来换取少量速度?如果您唯一的线程安全性依赖于 Vector 和的同步, code> Hashtable ,你几乎肯定会遇到麻烦。



通常,粗粒度操作需要同步 - 同步集合上的每一个小操作,并希望这足够好的方法几乎从来没有要求,所以为什么要采取小的打击?



如果你'在线程之间共享可变数据(比如集合)时,需要仔细考虑如何使用 Vector Hashtable

I'm updating an old project, and I'm wondering whether I should remove some objects marked as "obsolete" in the IDE. The objects in question are Vector and Hashtable. I've done some research, and it seems that the newer counterparts of these objects - ArrayList and HashMap - are basically the same, just not synchonized.

My question is, why should I make the switch? Aren't I just taking away a bit of security from my application in exchange for a small amount of speed? Seems like it's not a monumental decision either way, but I thought I'd see if it was worth the time before making a decision.

Thanks!

解决方案

Aren't I just taking away a bit of security from my application in exchange for a small amount of speed?

If your sole threading security is relying on the synchronization of Vector and Hashtable, you're almost certainly in trouble.

Usually, coarser-grained operations need to be synchronized - the "synchronize each small operation on the collections and hope that's good enough" approach is almost never what's required, so why take the small hit of having it at all?

If you're sharing mutable data (such as collections) between threads, you need to consider how to do so carefully - using Vector and Hashtable can give you a false sense of security.

这篇关于为什么像ArrayList和HashMap这样的类不同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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