- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个简短的问题。如果我有一个包含读/写操作的列表:
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/
我正在阅读《Java 并发实践》,这个案例对我来说有点不清楚。为什么这段代码是线程安全的? @ThreadSafe public class ListHelper { public List
在为 Collections.synchronizedList 编写 putIfAbsent 方法时,需要在访问列表期间提供显式锁定。 以下代码片段更详细地解释了它: class ListHelper
我不确定如何正确使用 Collections.synchronizedList() 实现。 我有这两个: public synchronized static List getOrderList
我有一个简短的问题。如果我有一个包含读/写操作的列表: private List _persistedFilesList = Collections.synchronizedList(new Arra
我正在阅读方法 Collections.synchronizedList() 的实现并且很困惑它是装饰器模式还是代理模式的例子? 最佳答案 绝对是装饰器。它用相同接口(interface)的不同实现包
我有一个关于同步列表的一般性问题。 可以说在构造函数中我正在创建一个列表 List synchronizedList = Collections.synchronizedList(list); 我有一
这个问题在这里已经有了答案: Collections.synchronizedList and synchronized (6 个答案) 关闭 6 年前。 我正在使用 Collections.Syn
CopyOnWritearraylist 和 Collections.synchronizedList(..) 有什么区别?什么时候应该优先选择一个。 最佳答案 CopyOnWriteArrayLis
List list = Collections.synchronizedList(new ArrayList()); synchronized (list) { list.add("messa
我想知道如何在以下情况下生成数组的克隆: private List testList = Collections.synchronizedList(new ArrayList()); ... publ
在多线程应用程序中,我们有一个在单例中处理列表的方法。要获取列表的快照,我们需要执行以下操作: public List swapList() { if (syncLinkedList.isEm
java.util.Collections.SynchronizedList 中方法的实现是在互斥体上使用同步。既然所有的方法中,完整的方法体都在synchronized block 下,为什么不能写
在阅读有关集合实现的 Oracle 教程时,我发现了以下句子: If you need synchronization, a Vector will be slightly faster than a
这个问题在这里已经有了答案: Vector vs Collections.synchronizedList(ArrayList) (1 个回答) 关闭 8 年前。 我想使用集合类在多线程中添加、删除
我在 dos.oracle.com 上找到了这个 public static List synchronizedList(List list) Returns a synchronized (thre
根据我的理解,并发集合类优于同步集合,因为并发集合类不会锁定整个集合对象。相反,它们锁定了集合对象的一小部分。 但是当我检查 CopyOnWriteArrayList 的 add 方法时,我们正在获取
我正在尝试使用两个线程将 String 值添加到 ArrayList。我想要的是,当一个线程添加值时,另一个线程不应该干扰,所以我使用了 Collections.synchronizedList 方法
请参阅以下代码: List list = Collections.synchronizedList(new ArrayList()); // ... synchronized
如果我的要求规定我对列表的大部分访问都是为了阅读和修改(如果有的话),那么为什么我不能只执行以下任一操作 同步modifyList方法并使用ArrayList。对 arraylist 的所有读取都将不
我知道ArrayList不是线程安全的,我见过很多人推荐在 (Collections.synchronizedList) 中转换它。我需要创建一个系统,将 ArrayList 转换为 (Collect
我是一名优秀的程序员,十分优秀!