场景
此处省略
- 解释
英文
Early classes of the Java API, such as Vector, Hashtable and StringBuffer, were synchronized to make them thread-safe. Unfortunately, synchronization has a big negative impact on performance, even when using these collections from a single thread. It is better to use their new unsynchronized replacements: ArrayList or LinkedList instead of Vector Deque instead of Stack HashMap instead of Hashtable StringBuilder instead of StringBuffer Noncompliant Code Example Vector cats = new Vector(); Compliant Solution ArrayList cats = new ArrayList(); Exceptions Use of those synchronized classes is ignored in the signatures of overriding methods. @Override public Vector getCats() {...}
中文
1.早期类的java API,如向量、哈希表和StringBuffer是同步的使它们线程安全。不幸的是,同步对性能有很大的 负面影响,甚至当从单个线程使用这些集合时。 2.考虑到多线程的问题,现在一般采用ArrayList、LinkedList代替Vector,使用Deque代替Stack,使用 StringBuilder代替StringBuffer等等(特殊场景下,可能需要用到线程安全类,java.util.concurrent)
学习Java的同学注意了!!!
学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:543120397 我们一起学Java!
转载请标明出处:项目代码优化(四)
文章来源: 项目代码优化(四)