简体   繁体   English

说内容被锁定是什么意思?

[英]What does it mean to say that a list is locked internally?

This code is from the book Effective Java 此代码来自Effective Java

Object[] snapshot = list.toArray();// Locks list internally

I am mainly interested in the comment here . 我主要对这里的评论感兴趣。 Does it make the list unmodifiable ? 它是否使列表unmodifiable What does it mean to say that a list is locked internally ? 说内容被锁定是什么意思? How long is this lock kept ? 这个锁有多长时间了? Is there a better alternative to convert a List to an array ? 有没有更好的替代方法将List转换为数组?

I would imagine that it means the list doesn't maintain a reference to the returned array, meaning that the array can be modified without affecting the original list from where it came. 我想这意味着它意味着列表不维护对返回数组的引用,这意味着可以修改数组而不影响它来自的原始列表。 Likewise, any modifications to the list won't be reflected in the array. 同样,对列表的任何修改都不会反映在数组中。

This is important in terms of thread safety, because it means you can iterate on the contents of the list from a thread-safe perspective, without worrying about another thread altering the sttae of the list in the meantime. 这在线程安全方面很重要,因为这意味着您可以从线程安全的角度迭代列表的内容,而不必担心另一个线程在此期间改变列表的内容。 In this sense the state of the list is "locked" in the returned array, no matter what changes are made to the list afterwards - you can see it as taking a snapshot. 从这个意义上说,列表的状态在返回的数组中被“锁定”,无论之后对列表进行了哪些更改 - 您都可以将其视为拍摄快照。

toArray(); doesn't alter the state of the list - so it doesn't make it unmodifiable or anything like that. 不会改变列表的状态 - 所以它不会使它不可修改或类似的东西。

Like the others said, I think that is about concurrency: 像其他人说的那样,我认为这是关于并发性的:

Text from javadoc of java.uitl.List 来自java.uitl.List的javadoc的文本

The returned array will be "safe" in that no references to it are maintained by this list. 返回的数组将是“安全的”,因为此列表不会保留对它的引用。 (In other words, this method must allocate a new array even if this list is backed by an array). (换句话说,即使此列表由数组支持,此方法也必须分配新数组)。 The caller is thus free to modify the returned array. 因此调用者可以自由修改返回的数组。

Its about thread safety - ie conversion of the list to Array will be thread safe 它关于线程安全 - 即将列表转换为Array将是线程安全的

Edit: 编辑:
In simplest way - you can take it as 以最简单的方式 - 您可以将其视为

  • when Thread one is converting List -> Array no other thread is allowed to alter the list till the time Thread one has not completed the conversion Thread one转换List - > Array时,不允许其他线程更改列表,直到Thread one没有完成转换

For those wondering where the "internal locking" takes place: 对于那些想知道“内部锁定”发生在哪里的人:

Please note that J. Bloch writes as an introduction for the given code: "For example, suppose you have a synchronized list (of the sort returned by Collections.synchroniedList ) (...)" 请注意J. Bloch写的是给定代码的介绍: “例如,假设您有一个同步列表(由Collections.synchroniedList返回的排序)(...)”

In that case toArray() really "locks internal" because the implementation of the synchronized list will do just that (with a mutex) preventing any modification by other threads while the decoupled array is created. 在这种情况下, toArray()确实“锁定内部”,因为同步列表的实现只会(使用互斥锁)阻止在创建解耦数组时由其他线程进行任何修改。

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

相关问题 当他们说http是无国籍时,这是什么意思 - what does it mean when they say http is stateless 说一个类型是“盒装”是什么意思? - What does it mean to say a type is “boxed”? 说接口也是一种类型是什么意思? - What does it mean to say that an interface is also a type? “锁定”在Java堆栈跟踪中意味着什么? - What does “locked” mean in a Java stack trace? StackTrace中“锁定的同步器数量= 1”是什么意思? - What does “Number of locked synchronizers = 1” in a StackTrace mean? 在java中说“实例变量没有被过度使用”是什么意思? - What does it mean to say “Instance variables are not over-rided” in java? 当您说“这种语言在JVM上运行”时,这实际上是什么意思? - What does it really mean when you say “This language runs on JVM”? 当我们说特定数据结构是缓存友好的时候意味着什么? - What does it mean when we say that a particular datastructure is cache friendly? 当我们说 ArrayList 不同步时,这意味着什么? - What does it mean when we say an ArrayList is not synchronized? 说“Java Modified UTF-8 Encoding”是什么意思? - What does it mean to say “Java Modified UTF-8 Encoding”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM