Search code examples
androidmultithreadingsynchronize

How do I synchronize this properly?


public void consumeResponse(OmwListResponse<T> response) {
    synchronized (response.getResultList()) { // XXX this isn't synchronized safely
        for (T t : response.getResultList()) {
            if (!cacheList.contains(t)) {
                cacheList.add(t);
            }
        }
    }
}

The the situation is I don't want anyone to chance response.getResultList() or cacheList until this method is done. How do I properly do this?


Solution

  • Create a lock object:

    private static final void LOCK = new Object();
    

    and synchronize on that.