Thread safety when iterating through an ArrayList using foreach
<p>I've got an <code>ArrayList</code> which is being instantiated and populated on the background thread (I use it to store the <code>Cursor</code> data). At the same time it can be accessed on the main thread and iterated through using foreach. So this obviously may result in throwing an exception.</p>

<p>My question is what's the best practice to make this class field thread-safe without copying it every time or using flags?</p>

<pre><code>class SomeClass {

    private final Context mContext;
    private List&lt;String&gt; mList = null;

    SomeClass(Context context) {
        mContext = context;
    }

    public void populateList() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                mList = new ArrayList&lt;&gt;();

                Cursor cursor = mContext.getContentResolver().query(
                        DataProvider.CONTENT_URI, null, null, null, null);
                try {
                    while (cursor.moveToNext()) {
                        mList.add(cursor.getString(cursor.getColumnIndex(DataProvider.NAME)));
                    }
                } catch (Exception e) {
                    Log.e("Error", e.getMessage(), e);
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                }
            }
        }).start();
    }

    public boolean searchList(String query) { // Invoked on the main thread
        if (mList != null) {
            for (String name : mList) {
                if (name.equals(query) {
                    return true;
                }
            }
        }

        return false;
    }
}
</code></pre>