Which Java Collections are synchronized(thread safe), which are not?

前端 未结 10 2128
礼貌的吻别
礼貌的吻别 2020-12-16 11:05

Which Java Collections are synchronized, which are not?

Example: HashSet is not synchronized

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 12:00

    There are three groups of Collections.

    • 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.

提交回复
热议问题