简体   繁体   English

当我们说 ArrayList 不同步时,这意味着什么?

[英]What does it mean when we say an ArrayList is not synchronized?

What does it mean when we say an ArrayList is not synchronized?当我们说 ArrayList 不同步时,这意味着什么?

Does it mean that if we declare an ArrayList in object scope, multiple threads accessing the objects have the opportunity to modify the list?是不是说如果我们在object scope中声明一个ArrayList,访问对象的多个线程都有机会修改列表?

What does it mean when we say an ArrayList is not synchronized? 当我们说ArrayList不同步时它意味着什么?

It means that accessing an ArrayList instance from multiple threads may not be safe (read, "may result in unexpected behavior" or "may not work as advertised"). 这意味着从多个线程访问ArrayList实例可能不安全(读取,“可能导致意外行为”或“可能无法按照所宣传的方式工作”)。

Further reading: 进一步阅读:

Does it mean that if we declare an ArrayList in object scope, multiple threads accessing the objects have the opportunity to modify the list? 这是否意味着如果我们在对象范围内声明一个ArrayList,访问对象的多个线程有机会修改列表?

Even if it would have been thread safe, multiple threads would be able to modify the list. 即使它是线程安全的,多线程也能够修改列表。

The difference is that if it's not thread safe and multiple threads access the list, all bets are off. 不同之处在于,如果它不是线程安全的并且多个线程访问列表,那么所有的赌注都是关闭的。 Saying that the class is not thread safe, is the same as adding "If accessed from one thread at a time, this method works as follows....." in front of every method description. 假设该类不是线程安全的,就像添加“如果一次从一个线程访问,此方法如下工作......”在每个方法描述前面。

Synchronized or not, an ArrayList can always be modified by multiple threads. 无论是否同步,ArrayList总是可以被多个线程修改。 Synchronization is about preventing concurrent access. 同步是关于防止并发访问。

With ArrayList (or Collections in general) there are two concurrency problems. 对于ArrayList(或一般的集合),存在两个并发问题。

First , there is method synchronization. 首先 ,有方法同步。 This means, all calls to methods of an ArrayList instance are synchronized. 这意味着,对ArrayList实例的方法的所有调用都是同步的。 So there is always only one method executed at a time. 因此,一次只能执行一个方法。 All other method calls that happen while the first method still computes are queued until the running method is completed. 在第一个方法仍然计算时发生的所有其他方法调用将排队,直到运行方法完成。

Method synchronization can be ensured by wrapping an ArrayList like this: 通过包装这样的ArrayList可以确保方法同步:

List list = Collections.synchronizedList(new ArrayList());

Example: assume two threads try to do the following at the same time: 示例:假设两个线程同时尝试执行以下操作:

list.add(0, "test");

If you have a synchronized list, you are guaranteed that the list afterwords starts with two "test" entries. 如果您有一个同步列表,则可以保证后面的列表以两个“测试”条目开头。 If the list is not synchronized, you might get a list with only one "test" entry... or other unexpected results. 如果列表未同步,您可能会得到一个只包含一个“测试”条目的列表...或其他意外结果。

Second , there is instance synchronization. 其次 ,有实例同步。 Here we not only prevent concurrent method calls, but we make sure that only one thread has access to the list object for a time. 这里我们不仅阻止并发方法调用,而且确保一段时间内只有一个线程可以访问列表对象。 This is important if you have pieces of logic that require that the list remains in an unchanged state until the logic is done. 如果您有一些逻辑要求列表保持未更改状态直到逻辑完成,这一点很重要。 For example iterating over lists. 例如,迭代列表。 You don't want other threads to add elements while you iterate over a list. 迭代列表时,您不希望其他线程添加元素。

This kind of synchronization is done by wrapping your piece of logic with a synchronized block: 这种同步是通过使用synchronized块包装您的逻辑来完成的:

synchronized(list) {
      for (Object o:list) {
         ...
      }
}

Does it mean that if we declare an ArrayList in object scope, multiple threads accessing the objects have the opportunity to modify the list ? 这是否意味着如果我们在对象范围内声明一个ArrayList,访问对象的多个线程有机会修改列表?

Yes. 是。 If multiple threads operate on it at the same time, it may result in unexpected behaviour 如果多个线程同时对其进行操作,则可能导致意外行为

It means that instances of ArrayList are not guaranteed to be threadsafe. 这意味着ArrayList的实例不保证是线程安全的。 This usually includes both read and write access. 这通常包括读写访问。 If you do it without external synchronization you can leave the object in stange states and get some hard to debug behavior. 如果在没有外部同步的情况下执行此操作,则可以使对象处于stange状态并获得一些难以调试的行为。

Being synchronized means that every operation is thread safe - if you use the same vector from two threads at the same time, they can't corrupt the state. 同步意味着每个操作都是线程安全的 - 如果你同时使用两个线程中的相同向量,它们就不会破坏状态。 However, this makes it slower. 但是,这会让它变慢。

If you are working in a single threaded environment (or the list is limited to a thread and never shared), use ArrayList. 如果您在单线程环境中工作(或者列表仅限于线程且从不共享),请使用ArrayList。 If you are working with multiple threads that share the same collection, either use Vector, or use ArrayList but synchronize in some other way (eg, manually or via a wrapper). 如果您正在使用共享同一集合的多个线程,请使用Vector,或使用ArrayList,但以其他方式同步(例如,手动或通过包装器)。

Being synchronized means that every operation is thread safe - if you use the same Array List from two threads at the same time, they can't corrupt the state. 同步意味着每个操作都是线程安全的 - 如果同时使用两个线程中的相同Array List,它们就不会破坏状态。 However, this makes it slower. 但是,这会让它变慢。

By default ArrayList is not synchronized, you can achieved that by synchronized keyword 默认情况下,ArrayList不同步,您可以通过synchronized关键字实现

ArrayList al=new ArrayList();

Collections.synchronized(al);

Accessing arraylist's instances from multiple threads is not safe considerable.从多个线程访问 arraylist 的实例是不安全的。 There are some alternate way to go there **Arraylist ** is not like linkdlist.有一些到 go 的替代方法 **Arraylist ** 不像 linkdlist。 They may sound similar but not made internally same.它们听起来可能相似,但内部结构不同。

Arraylist<datamodel> are;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我们说Hashtable或Vector是同步的时候意味着什么? - What does it mean when we say Hashtable or Vector is synchronized? 当我们说同步(实例字段)时,它意味着什么? - what does it mean when we say synchronized(instance field)..? 当他们说StringBuffer类已同步时,这到底是什么意思? 是否已声明该类的所有方法同步? - What exactly does it mean when they say that StringBuffer class is synchronized? Are all methods of that class declared synchronized? 在Vector / ArrayList的上下文中,synchronized是什么意思? - What does synchronized mean in context of Vector / ArrayList? 当我们说特定数据结构是缓存友好的时候意味着什么? - What does it mean when we say that a particular datastructure is cache friendly? 当我们说java中的Byte宽​​度为8位时,它是什么意思? - What does it mean when we say the width of Byte in java is 8 bit? 当我们在Liferay中说Group时,这是什么意思?Group由什么组成? 及其用途? - What does that mean when we say Group in Liferay and what all does Group consists of? and its uses? 当我们说 tomcat 服务器正在“运行”时,这意味着什么。 幕后到底发生了什么 - What does it mean when we say tomcat server is “running”. What exactly is running behind the scenes 当我们说“整数是不可变的”时,我们到底是什么意思 - what do we mean exactly when we say “ints are immutable” “同步”是什么意思? - What does 'synchronized' mean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM