搜索
java 并发实战 对象组合问题
PHP中文网
PHP中文网 2017-04-17 13:53:46
[Java讨论组]

第55页 程序清单4-8

public Map<String, Point> getLocations() {
        return Collections.unmodifiableMap(
                new HashMap<String, Point>(locations));
    }

代码上方有一句话:
其中只返回一个HashMap,因为getLocations并不能保证返回一个线程安全的Map。

getLocations到底是不是线程安全,希望对并发了解可以给予一些指点,谢谢

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(1)
ringa_lee

getLocations 非线程安全。但是``返回的集合是只读集合,故返回的map是线程安全的。
oracle官方文档解释如下:

public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m)
Returns an unmodifiable view of the specified map. This method allows modules to provide users with "read-only" access to internal maps. Query operations on the returned map "read through" to the specified map, and attempts to modify the returned map, whether direct or via its collection views, result in an UnsupportedOperationException.
The returned map will be serializable if the specified map is serializable.

Parameters:
m - the map for which an unmodifiable view is to be returned.
Returns:
an unmodifiable view of the specified map.
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号