I use Collections.Synchronizedlist() to make the arraylist stream safe. What I want to ask is the following code, safe for the stream, that is, deleting when the list is executed again from the end: -
pendingExecutionList = Collections.synchronizedList(new ArrayList<>(initialCapacity));
I am creating a list in the main thread. and adding to this list from different threads. But iteration and deletion are only performed from one scheduled thread, as shown below: -
for (int i = pendingExecutionList.size() - 1; i >= 0; i--) { if (someCondition(pendingExecutionList.get(i))) { process(pendingExecutionList.remove(i)); } }
The above code is executed by only one thread, and several threads are added to this list.
I want to avoid using an iterator over synchronized(list) as this is not fault tolerant.
source share