- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不确定如何正确使用 Collections.synchronizedList() 实现。
我有这两个:
public synchronized static List<CurrencyBox> getOrderList() {
return Collections.synchronizedList(orderList);
}
和
public static List<CurrencyBox> getOrderList() {
return Collections.synchronizedList(orderList);
}
据我所知,synchronizedList 确实返回 orderList 而不是副本,对吗?
所以如果我想保证原子操作,比如添加和删除,上面哪个实现是正确的?
Java9 有什么变化吗?或者它仍然是可行的方法,或者您有任何其他建议吗?
谢谢
最佳答案
在没有上下文的情况下很难判断,从提供的代码片段看,两者都不能保证原子操作。
文档指出:
Returns a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list.
因此,即使您同步该方法,您也将获得最好的保证,即不会有两个对象同时创建同步列表。
您需要用 Collections.synchronizedList
包装原始的 orderList
以开始并返回每次的存储结果。
private static List<CurrencyBox> orderList = Collections.synchronizedList(new ArrayList<CurrencyBox>());
public static List<CurrencyBox> getOrderList() {
return orderList
}
关于java - 正确使用 Collections.synchronizedList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45567791/
我正在阅读《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
我是一名优秀的程序员,十分优秀!