简体   繁体   English

在 ArrayList 线程安全上移动 object 的最佳方法是什么?

[英]What is the best way to move an object on an ArrayList thread safe?

I'm using an external library in Android that has an ArrayList of objects that is accessed from another thread.我在 Android 中使用了一个外部库,该库具有从另一个线程访问的对象的 ArrayList。 My problem is that I'd like to move some objects from this AarrayList in a way that does not cause null exception on the other thread.我的问题是我想以一种不会在另一个线程上导致 null 异常的方式从这个 AarrayList 中移动一些对象。

So, what I want to do is: I have an ArrayList ABCDEFGH, and I want to move F from its actual position to the first position, so the ArrayList will become FABCDEGH. So, what I want to do is: I have an ArrayList ABCDEFGH, and I want to move F from its actual position to the first position, so the ArrayList will become FABCDEGH. Note that I can't use swap for that because every other object is shifted right.请注意,我不能为此使用交换,因为其他所有 object 都向右移动。

A naive implementation is:一个天真的实现是:

list.remove(F);
list.add(0,F);

But this implementation cause null exception on the other thread because the other thread is looping the list by hand-written counted loop, so because of the race condition the thread loop can run between the removal and addition of F. I can't make the ArrayList tread safe because I do not have access to the external library.但是这个实现在另一个线程上导致 null 异常,因为另一个线程正在通过手写计数循环循环列表,所以由于竞争条件,线程循环可以在删除和添加 F 之间运行。我不能ArrayList 安全,因为我无权访问外部库。

Is there a better way to make this movement (from any position to any position of the Arraylist) without the race condition?有没有更好的方法可以在没有竞争条件的情况下进行此运动(从任何 position 到 Arraylist 的任何 position)? Note that the size of the ArrayList will not be modified.注意ArrayList的尺寸不会被修改。

If you own the loop and the swap code, you could synchronize both operations on the list.如果您拥有循环和交换代码,则可以同步列表上的两个操作。

// code for loop
for (int i = 0; i < length; i++) {
  synchronized(list) {
    letter = list.get(i);
  }
  // whatever else
}

// code for swap
 synchronized(list) {
   list.remove(F);
   list.add(0,F);
 }

Update更新

I am assuming that you need the loop thread to see the changes you make in your swap/loop code.我假设您需要循环线程来查看您在交换/循环代码中所做的更改。 Is that correct?那是对的吗?

If not, they you could simply clone/copy the list for your own use.如果没有,您可以简单地克隆/复制列表供您自己使用。

List copy = new ArrayList(list);
copy.remove(F);
copy.add(0,F);

Reorder the objects in the ArrayList, but without using add() , remove() or any other method that may alter the size.重新排序 ArrayList 中的对象,但不使用add()remove()或任何其他可能改变大小的方法。

It can be done, rather inelegantly, as follows:可以这样做,相当不优雅,如下所示:

Object temp = F;

for (int i = list.indexOf(F); i > 0; i--) {
    list.set(i, list.get(i - 1));
}

list.set(0, temp);

Note, however, that the loop in the other thread may encounter the objects in the wrong order, or the same object at different positions.但是请注意,另一个线程中的循环可能会遇到顺序错误的对象,或者相同的 object 在不同的位置。 And if the other thread adds or removes items, then this will not work.如果另一个线程添加或删除项目,那么这将不起作用。

I think if you cannot synchronize the list, you perhaps shouldn't be trying to modify it from your thread.我认为如果您无法同步列表,您可能不应该尝试从您的线程中修改它。

转换 ArrayList 列表的最佳方法是什么<object>到自定义 object java11 Spring 引导 2.1<div id="text_translate"><p> 我有 ArrayList 对象列表,其中对象可以是文本、BigDecimal、Long</p><pre> List&lt;Object&gt; data = { {"date","prize1","cardsNumers","cardObtained","totalCards","anotherPrize"}, {"2020-05-01",435345.67,43,56,67,345345345}, {"2020-03-01",45656.2,34,67,23,456456}, {"2020-02-01",56565,34,56,67,878987} }</pre><p> arrayList 中的第 1、2、3 等对象是第 0 个 arraylist 中提到的每个字段的值。 在如下表中,</p><p> <a href="https://i.stack.imgur.com/Tmdw2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Tmdw2.png" alt="在此处输入图像描述"></a></p><p> 我想从上面获取每个文件的总和并分配给下面自定义 Object 的同一字段,</p><pre> public class SumObject { private Date date; private BigDecimal sumPrize1; private BigDecimal sumCardsNumers; private long sumCardObtained; private long sumTotalCards; private long sumAnotherPrize; }</pre><p> 我通过遍历 List 中的 arrayList 来实现这一点,并通过 get(index) 方法获取值。 但我想知道是否有任何其他好的方法可以做到这一点。</p><p> 如果有人有任何建议,请发布答案或给我一个我可以阅读的文件的方向。</p><p> 提前致谢。</p></div></object> - what would be best way to convert List of ArrayList<Object> to a custom object java11 Spring boot 2.1

暂无
暂无

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

相关问题 将原始向量转换为安全列表类型的最佳方法是什么(Arraylist) - What is the best way to convert a raw vector to type safe list(Arraylist) 在 libGDX 中移动对象的最佳方法是什么 - What is best way to move an Object in libGDX 是 ConcurrentHashMap <integer, arraylist<object> &gt; 线程安全?</integer,> - Is ConcurrentHashMap<Integer, ArrayList<Object>> Thread safe? 将Object ArrayList转换为Double List的最佳方法是什么 - What would be the best way of converting Object ArrayList to Double List 在 Spring 中存储和更改全局应用程序属性的最佳方法是什么 以线程安全的方式启动 - What is the best way to store and change global app properties in Spring Boot in a thread safe way Java 7:在ServletContextListener中实现线程安全的可修改静态对象的最佳方法? - Java 7: best way to implement thread-safe modifiable static object in ServletContextListener? Kinesis:关闭工人的最佳/安全方法是什么? - Kinesis: What is the best/safe way to shutdown a worker? 转换 ArrayList 列表的最佳方法是什么<object>到自定义 object java11 Spring 引导 2.1<div id="text_translate"><p> 我有 ArrayList 对象列表,其中对象可以是文本、BigDecimal、Long</p><pre> List&lt;Object&gt; data = { {"date","prize1","cardsNumers","cardObtained","totalCards","anotherPrize"}, {"2020-05-01",435345.67,43,56,67,345345345}, {"2020-03-01",45656.2,34,67,23,456456}, {"2020-02-01",56565,34,56,67,878987} }</pre><p> arrayList 中的第 1、2、3 等对象是第 0 个 arraylist 中提到的每个字段的值。 在如下表中,</p><p> <a href="https://i.stack.imgur.com/Tmdw2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Tmdw2.png" alt="在此处输入图像描述"></a></p><p> 我想从上面获取每个文件的总和并分配给下面自定义 Object 的同一字段,</p><pre> public class SumObject { private Date date; private BigDecimal sumPrize1; private BigDecimal sumCardsNumers; private long sumCardObtained; private long sumTotalCards; private long sumAnotherPrize; }</pre><p> 我通过遍历 List 中的 arrayList 来实现这一点,并通过 get(index) 方法获取值。 但我想知道是否有任何其他好的方法可以做到这一点。</p><p> 如果有人有任何建议,请发布答案或给我一个我可以阅读的文件的方向。</p><p> 提前致谢。</p></div></object> - what would be best way to convert List of ArrayList<Object> to a custom object java11 Spring boot 2.1 Java8中需要非线程安全助手对象的并行方式的惯用方式是什么? - What's the idiomatic way in Java8 to do something parallel that requires a non-thread-safe helper object? 将arraylist项连接到线程的最佳方法以及其他方法 - Best way to connect an arraylist item to a thread and other way around
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM