Collections.synchronizedList serializes individual calls, but a multi-step iteration still needs an external lock on the returned wrapper.
Java synchronized LinkedList iteration: hold the wrapper lock for the whole scan
Protect the whole observation
An audit worker cannot safely assume that a synchronized size call and a later iterator traversal describe one queue state. Another writer can modify the list between those operations. Acquire the wrapper monitor before creating and consuming the iterator when the scan must be consistent with cooperating writers.
The fixture uses one thread to show the correct lock boundary. It does not claim that a deterministic single-thread run proves concurrency safety. Fail-fast behavior is a bug signal, not the synchronization mechanism.
Keep the backing list private
The wrapper works only when every participant uses the wrapper or the same monitor. If a caller retains and mutates the raw LinkedList directly, the wrapper cannot protect that path. A long scan also blocks writers, so take an intentional snapshot under the lock when later processing is slow.
Working program
import java.util.*;
public class AuditQueueSnapshot {
public static void main(String[] args) {
List<String> auditQueue = Collections.synchronizedList(new LinkedList<String>());
auditQueue.add("receipt-47");
auditQueue.add("receipt-82");
List<String> snapshot;
synchronized (auditQueue) {
snapshot = new ArrayList<String>(auditQueue);
}
System.out.println(snapshot);
}
}Output
[receipt-47, receipt-82]Cost and ownership
Copying n elements takes O(n) time and memory while holding the wrapper lock. Process the independent snapshot after releasing it; returning an iterator over the live list would carry the synchronization duty to the caller.
Common Mistakes
- Do not assume a synchronized wrapper makes an iterator automatically safe.
- Do not lock the original backing list while writers lock only the wrapper.
- Do not run slow remote work under the collection monitor.
Read next
Java LinkedList: operations, internals and failure cases, Java LinkedList fail-fast iterators: a bug signal, not safety, Java collection views: live wrappers, snapshots and shallow copies, Java ReentrantLock: protect a complete state transition.
