[英]Why listIterator of SynchronizedList needs external synchronization by the user?
Please refer to the following java source code : 请参考以下java源代码:
static class SynchronizedList<E>
extends SynchronizedCollection<E>
implements List<E> {
final List<E> list;
public boolean equals(Object o) {
synchronized (mutex) {return list.equals(o);}
}
public int hashCode() {
synchronized (mutex) {return list.hashCode();}
}
public ListIterator<E> listIterator() {
return list.listIterator(); //Must be manually synched by user
}
My question is why is the listIterator() not guarded by the mutex as with hashcode() and equals() method ? 我的问题是为什么listIterator()没有像使用hashcode()和equals()方法那样被互斥锁保护? Why did they design it so that it requires external synchronization by the user ? 为什么他们设计它需要用户进行外部同步?
If you suggest: 如果你建议:
public ListIterator<E> listIterator() {
synchronized(mutex) { return list.listIterator(); }
}
That won't help much. 这无济于事。
You'd synchronize the creation of the iterator itself, which might help against a few issues. 您将同步迭代器本身的创建 ,这可能有助于解决一些问题。 But you won't synchronize the use of the iterator. 但是你不会同步迭代器的使用 。 Changes can still be made to the list while you hold the iterator and - implementation depending - may even cause it to fail, because the list may be in an invalid internal state temporarily. 当你持有迭代器时仍然可以对列表进行更改,并且 - 实现依赖 - 甚至可能导致它失败,因为列表可能暂时处于无效的内部状态。
The main usage of ListIterator is not in obtaining it , but actually iterating it over for visiting individual elements in the list. ListIterator的主要用途不是获取它,而是实际迭代它以访问列表中的各个元素。 This is a stateful operation and is entirely done by the client and not by the class SynchronizedList
. 这是一个有状态操作,完全由客户端完成,而不是由SynchronizedList
类完成。 On the other hand , the methods equals()
and hashCode()
are entirely computed within the SynchronizedList
and do not require client to do much expect to take the returned values. 另一方面,方法equals()
和hashCode()
完全在SynchronizedList
计算,并且不要求客户端做很多期望获取返回的值。 Synchronizing the method to obtain the iterator is not much helpful as user1252434 pointed out. user1252434指出,同步方法以获取迭代器并没有太大帮助。
ListIterator is a classic example of using client-side locking as a strategy to ensure synchronization when the original class cannot provide for this. ListIterator是使用客户端锁定作为策略的典型示例,以确保在原始类无法提供此功能时进行同步。
There could be two possible reasons. 可能有两个原因。
We have three different Implementation of List
interface . 我们有三种不同的List
接口实现。 One is Vector
and other two are ArrayList
and LinkedList
. 一个是Vector
,另外两个是ArrayList
和LinkedList
。
One reason is If we are dealing with Vectors
then defiantly no synchronization
is required on Vector
because it is already thread-safe but if we are using ArrayList
and LinkedList
then we need to synchronize these Lists 一个原因是如果我们正在处理Vectors
那么在Vector
上不需要synchronization
,因为它已经是线程安全的但是如果我们使用ArrayList
和LinkedList
那么我们需要同步这些列表
Other reason is we can use SynchronizedList
class in Single-Threaded
Application if they would have made listIterator()
as Synchronized
then this is performance hit by unnecessary thread-safety
even in single-threaded
environment. 其他原因是我们可以在Single-Threaded
应用程序中使用SynchronizedList
类,如果他们将listIterator()
为Synchronized
那么即使在single-threaded
环境中,这也会受到不必要的thread-safety
影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.