Thread synchronization coordinates concurrent threads so they do not change shared state simultaneously. Without it, race conditions can cause exceptions, stopped threads, zombie threads, sluggishness, or app failure. section 5.1.1 section 5.2.2
Producers and consumers sharing an ArrayList
Two producer threads add values while two consumer threads read and remove them from a shared ArrayList. The unsynchronized version can throw ArrayIndexOutOfBoundsException and leave threads alive forever. Wrapping both producer and consumer operations in synchronized (Main.list) blocks permits only one thread at a time to perform the compound check-and-change operation. section 5.2.2
Investigate synchronization problems by connecting observable symptoms to runtime evidence:
- Inspect the Threads tab to identify alive threads and their statuses, then use a thread dump for deeper investigation. section 5.1.1
Synchronization prevents race conditions but introduces locks: waiting threads slow execution, and faulty locking can cause performance problems, freezes, deadlocks, or starvation. Profile first, then use lock profiling when sampling indicates contention. section 7.4









