I am using Collections.Synchronizedlist()
to make my arraylist
thread safe. What I want to ask is the following code thread-safe i.e. remove while iterating over list from the end:-
pendingExecutionList = Collections.synchronizedList(new ArrayList<>(initialCapacity));
I am creating the list in main thread. and adding to this list from different threads. But, iteration and removal is being done only from a single 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 a single thread while multiple threads are adding to this list.
I want to avoid using iterator over synchronized(list)
as that is not fail-safe.