Java ArrayList.add() method thread safe for purely parallel adding? [duplicate]

我的未来我决定 提交于 2019-12-19 04:24:15

问题


Consider a for-loop over a function that takes an ArrayList reference and adds an object to that ArrayList. I would now like to execute each function call in parallel.

Is the ArrayList.add() method thread safe if I don't care about the sequence the objects are added and no function reads or manipulates any ArrayList elements? So I only want to make sure that at the end of the parallel call all objects are added to the list.


回答1:


No, it's not thread-safe. Wrap your list using Collections.synchronizedList(), or use explicit synchronization when accessing the list.




回答2:


ArrayList.add() is not thread-safe. Even if you're not reading the list from other threads, you shouldn't rely on this logical assumption. Here's the definition of ArrayList.add():

public boolean add(E e) {
    ensureCapacity(size + 1);
    elementData[size++] = e;
    return true;
}

As an example of a problem that could arise without synchronization, the size attribute may be inconsistent after adding all elements. If you later try to get the number of elements, the result may not be correct.




回答3:


No, the add method uses a number of class level properties which have no thread safe control. Calling add() concurrently in multiple threads would be highly likely to cause problems.




回答4:


For that you can use CopyOnWriteArrayListsince ArrayList is not thread safe.

CopyOnWriteArrayList

EDIT: I'm just gonna quote Oracle to show you that this is what you need:

A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array. This is ordinarily too costly, but may be more efficient than alternatives when traversal operations vastly outnumber mutations, and is useful when you cannot or don't want to synchronize traversals, yet need to preclude interference among concurrent threads.




回答5:


No it is not thread safe.You have two options:

  1. Either use a different collection: CopyOnWriteArrayList
  2. Use explicit synchronization while making modifications to the array list in a multithreaded environment.



回答6:


It is not thread-safe. In the absence of synchronization, threads can trample on each other's modification of the list, or might not even see each other's changes to it. Either way, it will be corrupted.

You should get the code working first with Collections.synchronizedList or a synchronized statement around the add call.

If you find that the synchronization on every operation has too much overhead, the next alternative is to give each thread its own private ArrayList, and then periodically, or at the end, dump the contents of the private ArrayLists into the main ArrayList.



来源:https://stackoverflow.com/questions/27442402/java-arraylist-add-method-thread-safe-for-purely-parallel-adding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!