gpt4 book ai didi

java - Collections.synchronizedList 传递到构造函数中

转载 作者:太空宇宙 更新时间:2023-11-04 06:40:22 28 4
gpt4 key购买 nike

我有一个简短的问题。如果我有一个包含读/写操作的列表:

private List<String> _persistedFilesList = Collections.synchronizedList(new ArrayList<String>());

我在某处使用了代码块

new ArrayList<String>(_persistedFilesList);

这个 block 代码需要同步吗?我可以看到在new ArrayList的构造函数中,在java doc中提到了迭代器,并且所有涉及遍历列表的操作都需要在synchList中进行同步。但我不确定。

谢谢。

最佳答案

does this block code needs syncronization?

答案是肯定的(如果您需要同步访问您创建的新数组列表)。

您已使用另一个列表的元素创建了一个新的 ArrayList。

Java文档

 public ArrayList(Collection<? extends E> c)

Constructs a list containing the elements of the specified collection, in the order they are returned by the
collection's iterator.

因此,无论您传递的集合是否已同步,它都将是一个全新的列表。

关于java - Collections.synchronizedList 传递到构造函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24756183/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com