I have an ArrayList which is accessed by multiple threads. Main thread only clears the list and others add to it. I don't want the list get cleared while another thread is adding item to it. I want the list is locked while thread is adding item to it.
this is the code of adding thread:
synchronized (items)
{
int length = jsonArray.length();
if ((length > 0)) for (int i = 0; i < length; i++)
{
items.add(new Item(jsonArray.getJSONObject(i)));
}
}
But I don't use synchronized block for clearing. Is synchronized block necessary for clear too?