Java ArrayList/String/atomic 变量读取线程安全吗? [英] Is Java ArrayList / String / atomic variable reading thread safe?

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

问题描述

我一直在考虑这个&阅读却能找到绝对权威的答案.

I've been mulling this over & reading but can find an absolute authoritative answer.

我有几个由包含 ArrayLists、Strings & 的对象组成的深层数据结构.原始值.我可以保证这些结构中的数据不会改变(没有线程会改变列表、改变引用、改变原语的结构).

I have several deep data structures made up of objects containing ArrayLists, Strings & primitive values. I can guarantee that the data in these structures will not change (no thread will ever make structural changes to lists, change references, change primitives).

我想知道读取这些结构中的数据是否是线程安全的;即从对象中递归读取变量、迭代 ArrayLists 等以在不同步的情况下从多个线程中的结构中提取信息是否安全?

I'm wondering if reading data in these structures is thread safe; i.e. is it safe to recursively read variables from the objects, iterate the ArrayLists etc. to extract information from the structures in multiple threads without synchronization?

推荐答案

它不安全的唯一原因是,如果一个线程正在写入一个字段,而另一个线程同时从它读取.如果数据没有变化,则不存在竞争条件.使对象不可变是保证它们是线程安全的一种方法.首先阅读 IBM 的这篇文章.

The only reason why it wouldn't be safe is if one thread were writing to a field while another thread was simultaneously reading from it. No race condition exists if the data is not changing. Making objects immutable is one way of guaranteeing that they are thread safe. Start by reading this article from IBM.

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

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