是Java HashSet线程安全的只读吗? [英] is Java HashSet thread-safe for read only?

查看:446
本文介绍了是Java HashSet线程安全的只读吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在通过Collections.unmodifiableSet()运行它之后有一个HashSet的实例,它是否是线程安全的?

If I have an instance of an HashSet after I ran it through Collections.unmodifiableSet(), is it thread-safe?

推荐答案

从Javadoc:


请注意,此实现未同步。如果多个线程同时访问一个哈希集,并且至少有一个线程修改了该集合,那么它必须在外部同步。

Note that this implementation is not synchronized. If multiple threads access a hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally

不修改集,因此你很好。

Reading doesn't modify a set, therefore you're fine.

这篇关于是Java HashSet线程安全的只读吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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