哪些Java集合是同步的(线程安全的),哪些不是? [英] Which Java Collections are synchronized(thread safe), which are not?

查看:414
本文介绍了哪些Java集合是同步的(线程安全的),哪些不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪些Java集合是同步的?

Which Java Collections are synchronized, which are not?

示例:哈希集未同步

推荐答案

有三组Collection。

There are three groups of Collections.


  • Java 1.0集合主要是遗留类。这包括哈希表,向量,堆栈。它们是同步的,但我不建议您使用它们。属性也许是一个例外,但是我不会在多线程上下文中使用它。

  • 1998年添加的Java 1.2集合在很大程度上取代了这些集合,它们不是同步的,但是可以同步使用 Collections.synchronizedXxx()方法

  • 2004年添加的Java 5.0并发集合支持无锁,线程安全的集合。

  • Java 1.0 collections which mostly legacy classes. This includes Hashtable, Vector, Stack. These are synchronized but I don't recommend you use them. Properties is perhaps one exception, but I wouldn't use it in a multi-threaded context.
  • Java 1.2 collections added in 1998 which largely replaced these collection are not synchronized, but can be synchronized using Collections.synchronizedXxx() methods
  • Java 5.0 concurrency collections added in 2004 support lock free, thread safe collections.

简而言之,我建议您使用的所有集合都不是同步的。

In short, none of the collections I would recommend you use are synchronized.

这篇关于哪些Java集合是同步的(线程安全的),哪些不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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