I have one function: fillFromDB , which populates ArrayList instances called list from SQLite DB.
I also have another Thread that should clear this list and refill it.
I am having some problems with this list, because sometimes another Thread clears the list while the first one still fills it, raising an IndexOutOfBoundsException on the adapter.
// This occurs onCreate() fillFromDb(list); downloadAndRepopulateList(list); private void downloadAndRepopulateList(List list) { new Thread(new Runnable() { @Override public void run() { list.clear(); // Some other clode } } }
I know that the problem is that they are not thread safe, but I have never used synchronized before.
So my question is:
if I changed the download function as follows:
private void downloadAndRepopulateList(List list) { new Thread(new Runnable() { @Override public void run() { synchronized(list) { list.clear();
will it wait until UIThread completes filling the list with fillFromDb() and then continues to clear and re-fill it?
If not, how do I do this?
source share